// originals WeBid functions 
$(document).ready(function() {
	function window_open(pagina,titulo,ancho,largo,x,y){
		alert(0);
		var Ventana= 'toolbar=0,location=0,directories=0,scrollbars=1,screenX='+x+',screenY='+y+',status=0,menubar=0,resizable=0,width='+ancho+',height='+largo;
		open(pagina,titulo,Ventana);
	}
	$('a.new-window').click(function(){
		var posY = ($(window).height()-550)/2;
		var posX = ($(window).width())/2;
		window.open(this.href, this.alt, "toolbar=0,location=0,directories=0,scrollbars=1,screenX="+posX+",screenY="+posY+",status=0,menubar=0,width=550,height=550");
		return false;
	});
	$(function() {
		$('#gallery a').lightBox();
	});
//end originals WeBid functions

/**
* Styleswitch stylesheet switcher built on jQuery
* Under an Attribution, Share Alike License
* By Kelvin Luck ( http://www.kelvinluck.com/ )
**/
	$('.styleswitch').click(function() {
		switchStylestyle(this.getAttribute("rel"));
		return false;
		});
	var c = $.cookie('WBC_style');
	if (c) switchStylestyle(c);

	function switchStylestyle(styleName) {
		$('link[@rel*=style][title]').each(function(i) {
			this.disabled = true;
			if (this.getAttribute('title') == styleName) this.disabled = false;
			});
		$.cookie('WBC_style', styleName, { expires: 365});
	}
//End Styleswitch stylesheet switcher

//Increase, decrease and reset font size. Fisrt read cookie
	var fsz = $.cookie('WBC_fontsize');
    var options = { path: '/', expires: 10 };
	if (fsz) { var cookieFontSize = fsz; }
	else { var cookieFontSize = $('.wide.fs3').css('font-size'); }
	$('.wide.fs3').css('font-size', parseFloat(cookieFontSize, 10));
	// Reset Font Size
	$(".resetFont").click(function(){
	var originalFontSize = 12;
	$('.wide.fs3').css('font-size', originalFontSize);
	$.cookie('WBC_fontsize', originalFontSize, options);
	});
	// Increase Font Size
	$(".increaseFont").click(function(){
	var s = $.cookie('WBC_fontsize');	
	if (s) { var currentFontSize = s; }
	else { var currentFontSize = $('.wide.fs3').css('font-size'); }
	    var currentFontSizeNum = parseFloat(currentFontSize, 10);
    var newFontSize = currentFontSizeNum;
	if (newFontSize < 17) newFontSize = currentFontSizeNum*1.1;
    $('.wide.fs3').css('font-size', newFontSize);
	$.cookie('WBC_fontsize', newFontSize, options);
    return false;
	});
	// Decrease Font Size
	$(".decreaseFont").click(function(){
	var s = $.cookie('WBC_fontsize');
	if (s) { var currentFontSize = s; }
	else { var currentFontSize = $('.wide.fs3').css('font-size'); }
    var currentFontSizeNum = parseFloat(currentFontSize, 10);
    var newFontSize = currentFontSizeNum;
	if (newFontSize > 8) newFontSize = currentFontSizeNum*0.9;
    $('.wide.fs3').css('font-size', newFontSize);
	$.cookie('WBC_fontsize', newFontSize, options);
    return false;
	});
	
//Generating the subnav menu
	$("ul.subnav").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)  
	$("ul.topnav li span").click(function() { //When trigger is clicked...  
    //Following events are applied to the subnav itself (moving subnav up and down)  
    $(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click  
    $(this).parent().hover(function() {  
    }, function(){  
        $(this).parent().find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up  
        });  
    //Following events are applied to the trigger (Hover events for the trigger)  
    }).hover(function() {  
        $(this).addClass("subhover"); //On hover over, add class "subhover"  
        }, function(){  //On Hover Out  
            $(this).removeClass("subhover"); //On hover out, remove class "subhover"  
    });
	
//the rotating header image
	$('#wbc-header').cycle({
		fx: 'fade',
		speed: 5000, 
        before: onBefore
	}); 
 
    var slidesAdded = false; 
     
    function onBefore(curr, next, opts) { 
        // make sure we don't call addSlide before it is defined 
        if (!opts.addSlide || slidesAdded) 
            return; 
         
        // add slides for images 3 - n 
        // slides can be a DOM element, a jQuery object, or a string 
        for (var i=3; i < 5; i++) 
            opts.addSlide('<img src="themes/WBColors/img/banners/header'+i+'.jpg" width="942" height="350" />'); 
        slidesAdded = true; 
    }; 

//the view format in main page: list or thumbnails
	var ck_vm = ($.cookie('WBC_viewmode') == null) ? '' : $.cookie('WBC_viewmode');
	var ck_ltv = ($.cookie('WBC_ltv') == null) ? '' : $.cookie('WBC_ltv');
	$('a.switch_thumb').addClass(ck_vm);
	$('ul.display').addClass(ck_ltv);
	function addOrRemove() {
		if ($('a.switch_thumb').hasClass('swap')) {
			$('a.switch_thumb').removeClass('swap');
			$('ul.display').fadeOut('fast', function() {
				$(this).fadeIn('fast').removeClass('thumb_view');
			});
			$.cookie('WBC_viewmode', '', { expires: 365 });
			$.cookie('WBC_ltv', '', { expires: 365 });
		}
		else {
			$('a.switch_thumb').addClass('swap');
			$('ul.display').fadeOut('fast', function() {
				$(this).fadeIn('fast').addClass('thumb_view');
			});							
			$.cookie('WBC_viewmode', 'swap', { expires: 365 });
			$.cookie('WBC_ltv', 'thumb_view', { expires: 365 });
		}
	}
	function removeOrAdd() {
		if (!($('a.switch_thumb').hasClass('swap'))) {
			$('a.switch_thumb').addClass('swap');
			$('ul.display').fadeOut('fast', function() {
				$(this).fadeIn('fast').addClass('thumb_view');
			});							
			$.cookie('WBC_viewmode', 'swap', { expires: 365 });
			$.cookie('WBC_ltv', 'thumb_view', { expires: 365 });
		}
		else {
			$('a.switch_thumb').removeClass('swap');
			$('ul.display').fadeOut('fast', function() {
				$(this).fadeIn('fast').removeClass('thumb_view');
			});
			$.cookie('WBC_viewmode', '', { expires: 365 });
			$.cookie('WBC_ltv', '', { expires: 365 });
		}
	}
	$('a.switch_thumb').toggle(function(){
		addOrRemove();
	}, function () {
		removeOrAdd();
	});

//assigning the class active to the numnav and topnav menus according to the variables sort and items in the URL
	var path = location.pathname.substring(1);
	$('#wbc-splitmenu a[href$="' + path + '"]').addClass('active');
	if (path == 'WeBid/') $('#wbc-splitmenu a[href$="index.php"]').addClass('active');
	
	var itm = $(document).getUrlParam("items");  
	if (itm == null) {
		if ($.cookie('WBC_items') == null) itm = 10;
		else itm = $.cookie('WBC_items');
		}
	$('#numnav a[rel$=' + itm + ']').addClass('active');
  
	var sorted = $(document).getUrlParam("sort");
	if (sorted == null) {
		if ($.cookie('WBC_sorts') == null) sorted = 0;
		else sorted = $.cookie('WBC_sorts');
		}
	$('.topnav a[rel$=' + sorted + ']').addClass('active');
	if (sorted < 4) $('.topnav a[rel$=-1]').addClass('active');
	if ($('.topnav #allitems a').hasClass('active')) $('.topnav #allitems span').addClass('active');
	if ($('.topnav #buyitnow a').hasClass('active')) $('.topnav #buyitnow span').addClass('active');
	$.cookie('WBC_items', itm, { expires: 365});
	$.cookie('WBC_sorts', sorted, { expires: 365});
	
//generating the URL in the numnav and topnav menus
	$('a.itsort').each (function(){
		var theitem = $('a.nit.active').attr('rel');		
		var thesort = $(this).attr('href');
		$(this).attr('href', thesort + '&items=' + theitem);
	});
	$('a.nit').each (function(){
		var thesort = $('a.itsort.active').attr('rel');
		if (thesort == -1) thesort = 0;
		var theitem = $(this).attr('href');
		$(this).attr('href', theitem + '&sort=' + thesort);
	});
//preview the image in main page	
	$('p.preview a').imgPreview({
	srcAttr: 'rel',
    containerID: 'imgPreviewWithStyles',
	distanceFromCursor: {
                top: -200,
                left: 30
		},
    imgCSS: {
        // Limit preview size:
        height: 300
		},
    // When container is shown:
    onShow: function(link){
		$('<span>' + link.title + '</span>').appendTo(this);
        // Animate link:
        $(link).stop().animate({opacity:0.4});
        // Reset image:
        $('img', this).stop().css({opacity:0});
		},
    // When image has loaded:
    onLoad: function(){
        // Animate image
        $(this).animate({opacity:1}, 300);
		},
    // When container hides: 
    onHide: function(link){
		$('span', this).remove();
        // Animate link:
        $(link).stop().animate({opacity:1});
		}
	});
});
