var cur_obj, next_obj;
var slideshowTimer = null;
var J = jQuery.noConflict();
J(document).ready(function(){
	
	J('INPUT.make_purchase').click(function(){
		var address = J('INPUT[title=billingaddress]:eq(0)').val() + ', ' + J('INPUT[title=billingaddress]:eq(1)').val();
		J('INPUT[title=billingaddress]:last').val(address);	
		if (J('#fmChbx-shippingSameBilling').hasClass('checked')) {
			var address = J('INPUT[title=shippingaddress]:eq(0)').val() + ', ' + J('INPUT[title=shippingaddress]:eq(1)').val();	
			J('INPUT[title=shippingaddress]:last').val(address);	
		}
	});
	
	Cufon.set('fontFamily', 'trajan-pro').replace('.main-content H1');
	Cufon.set('fontFamily', 'avenirl').replace('.home-box H3')('.block-bg H3')('.steps A');

	J('form.NiceIt').NiceIt();
	
	J('#fmInp-wpsc_checkout_form_24').after('<span class="note">(De definitieve datum en opstijglocatie wordt telefoonisch besproken)</span>')
	
	J('#fmChbx-shippingSameBilling').click(function(){
		if (J('#fmChbx-shippingSameBilling').hasClass('checked')) {
		J('.wpsc_shipping_forms').css('display', 'table-row');
			J('.wpsc_shipping_forms').css('display', 'table-row');
		} else {
			
				J('.wpsc_shipping_forms').css('display', 'none');
			J('.wpsc_shipping_forms').css('display', 'none');
		}
	})

	J('.slider-right UL LI:first').addClass('active');
	
	J('.wp_dTreewp_dtree_Node A.node').each(function(i){
		var href = J(this).attr('href');
		var new_href = href+'?cat=3';
		
		if (J(this).attr('id')) {
			J(this).attr('href',new_href);
		}	
	})

	J('.comment-author').after('<div class="relax"></div>')
	J('.gastenboek #submit').val('REACTIE TOEVOEGEN');
	
	/* Main Menu structure */
	J('.main-menu UL LI A').each(function(i){
		var name = J(this).html();
		J(this).html('<span class="hover-left">&nbsp;</span><span class="hover-bg"><span>'+name+'</span></span><span class="hover-right">&nbsp;</span>');
		if ( J(this).parent('LI').children('UL').length ) {
			J(this).parent('LI').addClass('parent');
		}
	});
	J('.sub-menu').append('<li class="sub-menu-bottom">&nbsp;</li>');
	
	J('.sub-menu LI A').each(function(i){
		var name_sub = J(this).children('SPAN').children('SPAN').html();
		J(this).html(name_sub);
	});
	
	J('.foto UL LI A').each(function(i){
		var name = J(this).html();
		var title = J('#gallery-name').html();
		if (name == title) {
			J(this).parent('LI').addClass('current_page_item');
		}
	});
	
    auto_hint();
	
	/* Overlay */
	J('.link-lightbox').click(function(){
		pausing();
		if (slideshowTimer != null){
			clearTimeout(slideshowTimer);
			slideshowTimer = null;
		}
		
		var w = J(window).width();
		var h = J(document).height();
		J('.overlay').css('width', w).css('height', h).show();
		var l_w = J('.custom-lightbox').width();	
		
		var w_p = parseInt((w/2)-240);
		J('#custom-lightbox').css('left', w_p);
		J('#custom-lightbox .lightbox-wrapper').html(J('.custom-lightbox').html());
		J('#custom-lightbox').fadeIn(300);
		Cufon.replace('#custom-lightbox H2');
		
		initPlayer('wp-content/themes/xclusive/public',J(this).attr('rel'),'video');
		
		J('.overlay').click(function(){	
			J('.overlay').hide();
			J('#custom-lightbox').hide();
			resuming();
			//slideshowTimer = setTimeout('SlideShowGo();', 5000);
		});
		
		J('.close-lightbox').click(function(){	
			J('.overlay').hide();
			J('#custom-lightbox').hide();
			resuming();
			//slideshowTimer = setTimeout('SlideShowGo();', 5000);
		});
		
	});
	
		/* Overlay */
	J('.link-belmij-lightbox').click(function(){
		pausing();
		if (slideshowTimer != null){
			clearTimeout(slideshowTimer);
			slideshowTimer = null;
		}
		
		var w = J(window).width();
		var h = J(document).height();
		J('.overlay').css('width', w).css('height', h).show();
		var l_w = J('.custom-lightbox2').width();	
		
		var w_p = parseInt((w/2)-240);
		J('#custom-lightbox2').css('left', w_p);
		J('#custom-lightbox2 .lightbox-wrapper').html(J('.custom-lightbox2').html());
		J('#custom-lightbox2').fadeIn(300);
		Cufon.replace('#custom-lightbox H2');
		
		//initPlayer('wp-content/themes/xclusive/public',J(this).attr('rel'),'video');
		
		J('.overlay').click(function(){	
			J('.overlay').hide();
			J('#custom-lightbox2').hide();
			resuming();
			//slideshowTimer = setTimeout('SlideShowGo();', 5000);
		});
		
		J('.close-lightbox').click(function(){	
			J('.overlay').hide();
			J('#custom-lightbox2').hide();
			resuming();
			//slideshowTimer = setTimeout('SlideShowGo();', 5000);
		});
		
	});
	
	/* Slider */
	/*J('.slider ul li:first').addClass('current');
	slideshowTimer = setTimeout('SlideShowGo();', 5000);
	
	var img1 = J('#placeholder IMG:eq(0)').attr('src');
	var img2 = J('#placeholder IMG:eq(1)').attr('src');
	var img3 = J('#placeholder IMG:eq(2)').attr('src');
	J('.data-box:first').css('display', 'block');
	if ( J('body').hasClass('home') ) {
		slider(img1, img2, img3);
		carousel();
	}	
	*/
	var btn_next, btn_prev;
	if ((J.browser.msie) && (parseInt(jQuery.browser.version) == 6)) {
		btn_next = 'public/images/lb/btn-next.gif';
		btn_prev = 'public/images/lb/btn-prev.gif';
	} else {
		btn_next = 'http://xclusive.dutchwebdesign.nl/wp-content/themes/xclusive/public/images/lb/lb-next.png';
		btn_prev = 'http://xclusive.dutchwebdesign.nl/wp-content/themes/xclusive/public/images/lb/lb-prev.png';
	}
	
	/* Lightbox */
	try {
		if ((J.fn.lightBox) && (J('.gallery-list').length > 0)){
			J('.gallery-list a.lightbox').lightBox({
				imageLoading:	'/wp-content/themes/xclusive/public/images/lb/lightbox-ico-loading.gif',		
				imageBtnPrev:	btn_prev,			
				imageBtnNext:	btn_next,			
				imageBtnClose:	'public/images/lb/lightbox-btn-close.gif',		
				imageBlank:		'public/images/lb/lightbox-blank.gif',
				overlayOpacity: 0.7,
				txtImage: ''
			});
		}
	} catch(e){}
	
	/* Lightbox for single Post */
	try {
		if ((J.fn.lightBox) && (J('.gallery-icon').length > 0)){
			J('.gallery-icon a').lightBox({
				imageLoading:	'/wp-content/themes/xclusive/public/images/lb/lightbox-ico-loading.gif',		
				imageBtnPrev:	btn_prev,			
				imageBtnNext:	btn_next,			
				imageBtnClose:	'public/images/lb/lightbox-btn-close.gif',		
				imageBlank:		'public/images/lb/lightbox-blank.gif',
				overlayOpacity: 0.7,
				txtImage: ''
			});
		}
	} catch(e){}
	
	/* Subscribe form */
	var timer = setTimeout(function(){
		J('.gsom-optin-form INPUT[type=text]').addClass('auto-hint');
		J('.gsom-optin-form INPUT:eq(0)').attr('title','Naam');
		J('.gsom-optin-form INPUT:eq(1)').attr('title','Email');
		J('.gsom-optin-form INPUT[type=submit]').addClass('submit');
		auto_hint();
	}, 1000);
	
	// Submit the product form using AJAX
	J("form.product_form_checkout").submit(function() {

    // we cannot submit a file through AJAX, so this needs to return true to submit the form normally if a file formfield is present
    file_upload_elements = J.makeArray(J('input[type=file]', J(this)));
		if(file_upload_elements.length > 0) {
			return true;
		} else {
			form_values = J(this).serialize();
			// Sometimes jJ returns an object instead of null, using length tells us how many elements are in the object, which is more reliable than comparing the object to null
			if(J('#fancy_notification').length == 0) {
				J('div.wpsc_loading_animation',this).css('visibility', 'visible');
			}
			J.post( 'index.php?ajax=true', form_values, function(returned_data) {
				eval(returned_data);
				J('div.wpsc_loading_animation').css('visibility', 'hidden');
				
				if(J('#fancy_notification') != null) {
					J('#loading_animation').css("display", 'none');
					//J('#fancy_notificationimage').css("display", 'none');
				}
				var amount = J('#amount').val();
								
				if (amount) {
					if ((amount > 24) && (amount)) {
						var loc = J('#wpsc_ajax_redirect_checkout').attr('value');
						loc = loc + amount;
						window.location = loc;
					} else {
						J('.producttext .error').fadeIn(500);		
					}	
				} else {window.location = J('#wpsc_ajax_redirect_checkout').attr('value');}
			});
			wpsc_fancy_notification(this);
			return false;
		}
	});
	
	J('#dtreea A').each(function(i){
		var t = J(this).html();
		if (J(this).html() == 'January') {J(this).html('Januari')}
		if (J(this).html() == 'February') {J(this).html('Februari')}
		if (J(this).html() == 'March') {J(this).html('Maart')}
		if (J(this).html() == 'April') {J(this).html('April')}
		if (J(this).html() == 'May') {J(this).html('Mei')}
		if (J(this).html() == 'June') {J(this).html('Juni')}
		if (J(this).html() == 'July') {J(this).html('Juli')}
		if (J(this).html() == 'August') {J(this).html('Augustus')}
		if (J(this).html() == 'September') {J(this).html('September')}
		if (J(this).html() == 'October') {J(this).html('Oktober')}
		if (J(this).html() == 'November') {J(this).html('November')}
		if (J(this).html() == 'December') {J(this).html('December')}
	});
	
	if ( J('body').hasClass('home') ) {
		var pause = setTimeout(function(){
			J('#placeholder').removeClass('loading');
			J('.slide:first').fadeIn(300);
			carousel();
		}, 1000);
	}
	
});

function initPlayer(path,file,id){
	var height = 340;
	var width = 480;
	if ( (J.browser.msie) && (parseInt(J.browser.version) <= 6) ) {
		var height = 305;
		var width = 530;
	}
		var s1 = new SWFObject(path+'/swf/player.swf','pimov',width,height,'7');
		s1.addParam('allowfullscreen','true');
		s1.addVariable('file', file);
		s1.addVariable('width', width);
		s1.addVariable('height', height);
		s1.addVariable('type','flv');
		s1.addVariable('autostart', true);
		s1.write(id);
}

function SlideShowGo(){

	if (slideshowTimer != null){
		clearTimeout(slideshowTimer);
		slideshowTimer = null;
	}
	
	cur_obj = J('.slider ul li.current');
	if (J('.slider ul li:last').hasClass('current')) {
		next_obj = J('.slider ul li:first');
	} else next_obj = cur_obj.next();
	
	next_obj.animate({top: '0'}, 1990);
	
	if ((J.browser.msie) && (parseInt(jQuery.browser.version) == 6)) {
		cur_obj.animate({top: '-350px'}, 2000, function(){
			J(this).css('top', '350px').removeClass('current');
			next_obj.addClass('current');
		});
	} else {
		cur_obj.animate({top: '-350px', opacity: '0'}, 2000, function(){
			J(this).css('top', '350px').css('opacity', '1').removeClass('current');
			next_obj.addClass('current');
		});
	}
	
	slideshowTimer = setTimeout('SlideShowGo();', 5000);
		
}

function slider(img1, img2, img3){
	
	J('#data').cycle({ 
		fx:     'fade', 
		speed:   3000, 
		timeout: 500, 
		pause:   0
	});	
	
	J('#placeholder').crossSlide({
		  fade: 4
		}, [
		  {
			src:  img1,
			from: 'bottom left',
			to:   '100% 0% 1.5x',
			time: 20
		  }, {
			src:  img2,
			from: 'top left',
			to:   'bottom right 1.5x',
			time: 20
		  }, {
			src:  img3,
			from: '100% 80% 1.5x',
			to:   '70% 0% 1.2x',
			time: 20
		  }
		],function(idx, img, idxOut, imgOut) {
			if (idxOut == undefined)
			{
				J('#data').cycle('pause');
			} else {
				J('#data').cycle('resume');
			}
		});
}
function carousel(){
	J('#placeholder').cycle({ 
		fx:     'fade', 
		speed:   3000, 
		timeout: 3000, 
		pause:   0
	});	
}
function pausing(){J('#placeholder').cycle('pause')}
function resuming(){J('#placeholder').cycle('resume')}

function auto_hint(){
	J('INPUT.auto-hint').each(function(i, el){
        if(J(this).val() == ''){
            J(this).val(J(this).attr('title'));
        }
        J(el).focus(function(){
            if (J(this).val() == J(this).attr('title')) {
                J(this).val('');
            }       
        });
        J(el).blur(function(){
            if (J(this).val() == '') {
                J(this).val(J(this).attr('title'));
            }
        });
    });
}

function externalLinks() {
  var d = document;
  if (!d.getElementsByTagName) return;
  var links = d.getElementsByTagName("a");
  for (var i=0; i<links.length; i++) {
    var link = links[i];
    if (link.getAttribute("href") && (link.getAttribute("rel") != null) && link.getAttribute("rel").indexOf("external") >= 0 ){
      link.target = "_blank";
    }
  }
}
window.onload = externalLinks;
