/* 
@Post type slide
*/
jQuery(function() {
	jQuery('#post-portfolio-slide').cycle({
		fx:  'scrollLeft',
		speed:  '500', 
		timeout: '3000',
		prev:   '#prev',
		next:   '#next'
	});
});



/* 
@Resize Large Images
*/
jQuery(document).ready(function(){
	
	var image = jQuery('.right-side .post-text img');
	var size = 600;
	
	for (i=0; i<image.length; i++) {
		var bigWidth = image[i].width;
		var bigHeight = image[i].height;
	
		if (bigWidth > size) {	
			var newHeight = bigHeight*size/bigWidth;
			image[i].width = size;
			image[i].height = newHeight;
		}
	}
});

jQuery(document).ready(function(){
	
	var image = jQuery('.left-side .post-text img');
	var size = 600;
	
	for (i=0; i<image.length; i++) {
		var bigWidth = image[i].width;
		var bigHeight = image[i].height;
	
		if (bigWidth > size) {	
			var newHeight = bigHeight*size/bigWidth;
			image[i].width = size;
			image[i].height = newHeight;
		}
	}
});

jQuery(document).ready(function(){
	var size = 960;
	var image = jQuery('.fullwidth .post-text img');
	
	for (i=0; i<image.length; i++) {
		var bigWidth = image[i].width;
		var bigHeight = image[i].height;
	
		if (bigWidth > size) {	
			var newHeight = bigHeight*size/bigWidth;
			image[i].width = size;
			image[i].height = newHeight;
		}
	}
});


/* 
@Scroll Top
*/
jQuery(document).ready(function(){
	jQuery('.divider a').click(function(){
		jQuery('html, body').animate({scrollTop:0}, 'slow');
		return false;
	});
});


/* 
@Tabs
*/
jQuery(document).ready(function(){
	jQuery('.tab ul.tabs li:first-child a').addClass('current');
	jQuery('.tab .tab-content div.tabs-tab:first-child').show();
	jQuery('.tab ul.tabs li a').click(function(e){
		$tab = jQuery(this).parent().parent().parent();
		$tab.find('ul.tabs').find('a').removeClass('current');
		jQuery(this).addClass('current');
		var $index = jQuery(this).parent().index();
		$tab.find('.tab-content').find('div.tabs-tab').not('div.tabs-tab:eq('+$index+')').slideUp();
		$tab.find('.tab-content').find('div.tabs-tab:eq('+$index+')').slideDown();
		e.preventDefault();
	})
});


/* 
@Toggle
*/
jQuery(document).ready(function(){
	jQuery('.togg a.tog').click(function(i){
		var dropDown = jQuery(this).parent().find('.tog-content');
		jQuery(this).parent().find('.tog-content').not(dropDown).slideUp();
		if (jQuery(this).hasClass('current')){
			jQuery(this).removeClass('current');
		} else { 
			jQuery(this).addClass('current');
		}
		dropDown.stop(false,true).slideToggle().css({display:'block'});
		i.preventDefault();
	})
});


/* 
@Accordion
*/
jQuery(document).ready(function(){
	jQuery('.accordion a.tog').click(function(i){
		if (jQuery(this).hasClass('current')){ 
			jQuery(this).removeClass('current');
		} else { 
			jQuery(this).parent().parent().find('.tog').removeClass('current');
			jQuery(this).addClass('current');
		}
		var dropDown = jQuery(this).parent().find('.tog-content');
		jQuery(this).parent().parent().find('.tog-content').not(dropDown).slideUp();
		dropDown.stop(false,true).slideToggle().css({display:'block'});
		i.preventDefault();
	})
});



/* 
@Open links in new window
*/
jQuery(function() {
	jQuery('a[rel*=external]').click( function() {
		window.open(this.href);
		return false;
	});
});


/* 
@Flickr Image Support Lightbox 
*/
jQuery(document).ready(function() {
	jQuery("#flickr_badge_wrapper a").each(function(i){
		var src = jQuery(this).find("img").attr("src");
		var title = jQuery(this).find("img").attr("title");
		var src2 = src.replace(/_s.jpg/g, ".jpg");
		jQuery(this).removeAttr("href");
		jQuery(this).attr({
			href: src2,
			title: title,
			rel: 'thumbcolorbox'
		});
	});
});


/* 
@Post Text Image Support Lightbox 
*/
/*jQuery(document).ready(function() {
	jQuery(".post-text a").each(function(){
	 	var src1 = jQuery(this).find("img").attr("src");
		var title = jQuery(this).find("img").attr("title");
		var src2 = '"'+src1+'"';
		var src3 = src2.slice(-5,-1);
		if(src3=='.jpg')
		{
			jQuery(this).removeAttr("href");
			jQuery(this).attr({
				href: src1,
				title: title,
				rel: 'slidecolorbox'
			});			
		}
	});
});
*/


/* 
@Gallery Image Support Lightbox 
*/
jQuery(document).ready(function($){
		$(".gallery").each(function(index, obj){
			var galleryid = Math.floor(Math.random()*10000);
			$(obj).find("a").colorbox({rel:galleryid, maxWidth:"95%", maxHeight:"95%"});
		});
		//$("a.lightbox").colorbox({maxWidth:"95%", maxHeight:"95%"});
});




/*
@Image Magnifying Glass / Image Hovers
*/
jQuery(document).ready(function(){
		jQuery(".thumb-fade a").append("<span></span>");
		jQuery(".thumb-fade img").hover(function(){
		jQuery(this).fadeTo(500, 0.4); // This should set the opacity to 100% on hover
		},function(){
		jQuery(this).fadeTo(800, 1.0); // This should set the opacity back to 60% on mouseout
		});
});


/*
@Create a Visual Image Preloader using jQuery
*/
jQuery(function () {
		jQuery('.thumb-loader img').hide();//hide all the images on the page
		});
		
		var i = 0;//initialize
		var int=0;//Internet Explorer Fix
		jQuery(window).bind("load", function() {//The load event will only fire if the entire page or document is fully loaded
			var int = setInterval("doThis(i)",300);//500 is the fade in speed in milliseconds
		});

		function doThis() {
			var imgs = jQuery('.thumb-loader img').length;//count the number of images on the page
			if (i >= imgs) {// Loop the images
				clearInterval(int);//When it reaches the last image the loop ends
		}
		jQuery('.thumb-loader img:hidden').eq(0).fadeIn(200);//fades in the hidden images one by one
		i++;//add 1 to the count
}


/*
@//Examples of how to assign the ColorBox event to elements
*/
jQuery(document).ready(function(){
			jQuery("a[rel='thumbcolorbox']").colorbox({transition:"fade"});
			jQuery("a[rel='videocolorbox']").colorbox({iframe:true, innerWidth:620, innerHeight:350});
			jQuery("a[rel='slidecolorbox']").colorbox({transition:"fade", slideshow:true});
			jQuery(".wp-caption a").colorbox({transition:"fade", slideshow:true});
});

/*
@Contact Form
*/
jQuery(document).ready(function(){
		 jQuery('#contact').ajaxForm(function(data) {
		 if (data==1){
			 jQuery('#success').fadeIn("slow");
			 jQuery('#bademail').fadeOut("slow");
			 jQuery('#badserver').fadeOut("slow");
			 jQuery('#contact').resetForm();
		 }else if (data==2){
			 jQuery('#badserver').fadeIn("slow");
		 } else if (data==3){
			 jQuery('#bademail').fadeIn("slow");
		}
		});
});

/*
@Contact Form
*/
jQuery(document).ready(function(){
		 jQuery('#page-contact').ajaxForm(function(data) {
		 if (data==1){
			 jQuery('#success-page').fadeIn("slow");
			 jQuery('#bademail-page').fadeOut("slow");
			 jQuery('#badserver-page').fadeOut("slow");
			 jQuery('#page-contact').resetForm();
		 }else if (data==2){
			 jQuery('#badserver-page').fadeIn("slow");
		 } else if (data==3){
			 jQuery('#bademail-page').fadeIn("slow");
		}
		});
});

/*
@ Drop Menu
*/
 jQuery(function(){
		jQuery('#primary-menu ul.drop-menu').superfish({
			delay:  1000,                        
			animation	: {height:'show'},
			speed:  300,                       
			autoArrows	: true,                      
			dropShadows: false      
		});
});


// JavaScript Document

/* 
 * Cross-browser event handling, by Scott Andrew
 */
function addEvent(element, eventType, lamdaFunction, useCapture) {
    if (element.addEventListener) {
        element.addEventListener(eventType, lamdaFunction, useCapture);
        return true;
    } else if (element.attachEvent) {
        var r = element.attachEvent('on' + eventType, lamdaFunction);
        return r;
    } else {
        return false;
    }
}

/*
 * Clear Default Text: functions for clearing and replacing default text in
 * <input> elements.
 *
 * by Ross Shannon, http://www.yourhtmlsource.com/
 */

addEvent(window, 'load', init, false);

function init() {
    var formInputs = document.getElementsByTagName('input');
    for (var i = 0; i < formInputs.length; i++) {
        var theInput = formInputs[i];
        
        if (theInput.type == 'text' && theInput.className.match(/\bcleardefault\b/)) {  
            /* Add event handlers */          
            addEvent(theInput, 'focus', clearDefaultText, false);
            addEvent(theInput, 'blur', replaceDefaultText, false);
            
            /* Save the current value */
            if (theInput.value != '') {
                theInput.defaultText = theInput.value;
            }
        }
    }
}

function clearDefaultText(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;
    
    if (target.value == target.defaultText) {
        target.value = '';
    }
}

function replaceDefaultText(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;
    
    if (target.value == '' && target.defaultText) {
        target.value = target.defaultText;
    }
}
