// JavaScript Document
jQuery(window).load(function(){
	var URL = "http://www.conducthq.com/";
	// Lazy Load images below the fold
	jQuery("#content img").lazyload({
		effect : "fadeIn",
		threshhold: 300
	}); 
	
	//Portfolio menu + pagination
	if(jQuery('.portfolio li.cat-item').length > 0) {
		
		jQuery('.portfolio li.cat-item').each(function(){
			//alert(cat);		
			//url=jQuery(this).find('a').attr('href');
			//temp=url.split('=');
			//cat=temp[1];
			//jQuery(this).find('a').attr('href','?page_id=7&cat='+cat);
			
			var url=jQuery(this).find('a').attr('href');
			var temp=url.split('/');
			var cat=temp[temp.length-1];
			//alert(cat);
			jQuery(this).attr('id',cat);
			jQuery(this).find('a').attr('href',URL + 'folio?cat=' + cat);
			//jQuery(this).find('a').attr('href','#');
		});
		
		function subNavLinks() {
			jQuery('.portfolio li.cat-item').click(function(){
				jQuery('.portfolio li.cat-item').unbind("click");
				//document.location='index.php?page_id=7&cat='+cat;
				jQuery('#loader').fadeIn();
				var category=jQuery(this).attr('id');
				//alert( URL + 'folio-ajax?cat='+category);
				jQuery.ajax({
					url: URL + 'folio-ajax?cat='+category,
					type: 'GET',
					//timeout: 3000,
					error: function(){
						alert('Try again please.');
						subNavLinks();
					},
					success: function(html){
						//alert(html);
						jQuery('#content').fadeOut();
						jQuery('#content').html(html).fadeIn(200,function(){
							paginator();
							slidingPanels();
							subNavLinks();
							jQuery('#content .portfolio').each(function(i){
								jQuery(this).animate({opacity:1},i*250).fadeIn(500);
								if(jQuery('#content .portfolio').is(".portfolio:last")) { jQuery('#loader').fadeOut(); }
							});
						});
					}
				});
				return false; 
			});
		}
		
		function paginator() {
			jQuery('.ajax-paginator').click(function(){
				jQuery('.ajax-paginator').unbind("click");
				//document.location='index.php?page_id=7&cat='+cat;
				jQuery('#loader').fadeIn();
				temp=jQuery(this).attr('id');
				temp=temp.split('-');
				jQuery.ajax({
					url: URL + 'folio-ajax?cat='+temp[1]+'&off='+temp[2],
					type: 'GET',
					//timeout: 3000,
					error: function(){
						alert('Try again please.');
						paginator();
					},
					success: function(html){
						//alert(html);
						jQuery('#content').fadeOut();
						jQuery('#content').html(html).fadeIn(200,function(){
							paginator();
							slidingPanels();
							jQuery('#content .portfolio').each(function(i){
								jQuery(this).animate({opacity:1},i*250).fadeIn(500);
								if(jQuery('#content .portfolio').is(".portfolio:last")) { jQuery('#loader').fadeOut(); }
							});
						});
					}
				});
				return false;
			});
		}
		
		// The magic sliding panels
		function slidingPanels() {
			if(jQuery.browser.msie) { 
				jQuery('.entry-content a span.slide-title').parent('a').append('<span class="cover-up"></span>');
			} else {
				jQuery('.entry-content a span.slide-title').css({ opacity : '0' }).parent('a').append('<span class="cover-up"></span>');
			}
			jQuery('.portfolio').find('.item').hover(function(e){
				if(jQuery(this).find('img.thumbnail').queue().length == 0) {
					if(jQuery.browser.msie) {
						jQuery(this).find('img.thumbnail').animate({ marginTop : '-25px' }, 100).parent('a').find('span.slide-title').show();
					} else {
						jQuery(this).find('img.thumbnail').animate({ marginTop : '-25px' }, 100).parent('a').find('span.slide-title').fadeTo(300,1.0); 
					}
				}				
			},
			function(e){
				if(jQuery.browser.msie) { 										  
					jQuery(this).find('img.thumbnail').animate({ marginTop : '0' }, 100).parent('a').find('span.slide-title').hide();
				} else {
					jQuery(this).find('img.thumbnail').animate({ marginTop : '0' }, 100).parent('a').find('span.slide-title').fadeTo(300,0.0);
				}
			});
		
		}
		
		jQuery('#loader').fadeIn();
		paginator();
		slidingPanels();
		subNavLinks();
		
		jQuery('#content .portfolio').each(function(i){
			jQuery(this).animate({opacity:1},i*250).fadeIn(500);
			if(jQuery('#content .portfolio').is(".portfolio:last")) { jQuery('#loader').fadeOut(); }
		});
	}
	
	//Single Post menu + pagination
	if(jQuery('img.thumb').length>0) {
		function changeSlide(obj){
			var img=jQuery(obj).attr('id').split('-');
			//alert(img[1]);
			jQuery('#large-images img').each(function(){
				if(jQuery(this).css('display') != 'none') {
					var fimg = jQuery(this).attr('id').split('-');
					jQuery('#thumb-'+fimg[1]).animate({opacity:1},250).one('click', function(){changeSlide(this);});
					jQuery(this).fadeOut(400,function(){
						jQuery('#large-'+img[1]).fadeIn(400);
						jQuery('#thumb-'+img[1]).animate({opacity:0.5},250);						
					});
					
				}
			});
		}
				
		jQuery('img.thumb').one('click', function(){
			jQuery('img.thumb').show();
			changeSlide(jQuery(this));
		}).css('cursor','pointer');
		
		jQuery('#thumb-1').animate({opacity:0.5},250).unbind('click');
	}
	
});
