/* User Scripts */
jQuery(document).ready(function(){
	jQuery('#navigation li li.current_page_item').parent().parent().addClass('current_page_item'); //Navigation
	jQuery('#navigation li li li.current_page_item').parent().parent().parent().parent().addClass('current_page_item'); //Navigation
	jQuery('#home .widget:last, #bottom .widget:last, #home .widget:last h3').css('background', 'none'); //Home and Bottom
	jQuery('#bottom .widget #recent-widget p:last').css('padding', '0'); //Bottom
	jQuery('#middle .post:last').css('background', 'none'); //Middle
	jQuery('#middle hr:last').remove(); //Middle
	jQuery('blockquote').prepend('<div class="blq"></div>'); //Middle
	jQuery('#middle a img.size-thumbnail').parent().attr({rel: 'prettyPhoto[content]'}); //Middle Lightbox Images
	jQuery('.widget_custom_flickr_entries .flickr_badge_image:eq(2), .widget_custom_flickr_entries .flickr_badge_image:eq(5), .widget_custom_flickr_entries .flickr_badge_image:eq(8), .widget_custom_flickr_entries .flickr_badge_image:eq(11), .widget_custom_flickr_entries .flickr_badge_image:eq(14), .widget_custom_flickr_entries .flickr_badge_image:eq(17), .widget_custom_flickr_entries .flickr_badge_image:eq(20)').css('padding-right', '0'); //Flickr Widget
	jQuery('.widget_custom_advertisement_entries .adv_widget_image:eq(1), .widget_custom_advertisement_entries .adv_widget_image:eq(3), .widget_custom_advertisement_entries .adv_widget_image:eq(5)').css('padding-right', '0'); //Advertisement Widget
	var $ul = jQuery('.widget_custom_recent_news_entries ul:last-child').find('li').length; if ( $ul == 0 ) { jQuery('.widget_custom_recent_news_entries ul:last-child').remove(); } //Recent News Widget
	jQuery('a.comment-reply-link').addClass('all').removeClass('comment-reply-link'); //Comments
	jQuery('#middle input:text').css('border-radius', '3px').css('-moz-border-radius', '3px').css('-webkit-border-radius', '3px'); //Validator
	jQuery('#middle textarea').css('border-radius', '3px').css('-moz-border-radius', '3px').css('-webkit-border-radius', '3px'); //Validator
	jQuery('.pp_gallery ul a').css('border', '1px rgba(0,0,0,0.5) solid'); //Validator
	jQuery('.coin-slider').css('zoom', '1'); //Slider
	if ( jQuery('code').length > 0 ) { jQuery('code').each(function(i){ jQuery(this).html(jQuery(this).html().replace(/{/g, '[').replace(/}/g, ']')); }); } //Code
});



/* Navigation */
jQuery(document).ready(function(){ jQuery('#header div.menu ul').attr({ id: 'navigation' }); });

jQuery(document).ready(function(){
	var $i = $j = $m = $n = 0;
	jQuery.each(jQuery('#navigation li'), function() { $i = $i + 1; });
	jQuery.each(jQuery('#navigation li li'), function() { $m = $m + 1; });
	$i = $i - $m;
	$j = (980 / $i) + ((100 / ($i - 1)) / 100) - 2;
	$j = ($j).toPrecision(7);
	jQuery('#navigation li').css('width', $j + 'px');
	jQuery.each(jQuery('#navigation li li'), function() { jQuery(this).css('width', '225px'); });
	jQuery.each(jQuery('#navigation li li li'), function() { jQuery(this).css('width', '225px'); });
	jQuery('#navigation li:last-child').css('background', 'none').css('padding-right', '0');
	jQuery('#navigation li li').css('background', 'none').css('padding-right', '0');
	jQuery('#navigation li li li').css('background', 'none').css('padding-right', '0');
});

jQuery(document).ready(function(){
	jQuery('#navigation li ul').parent().addClass('prnt');
	jQuery('#navigation li.prnt ul').css('width', '225px');
	jQuery('#navigation li li ul').parent().removeClass('prnt').addClass('prnt2');
	jQuery('#navigation li ul li:not(:first-child)').css('border', '0');
	jQuery('#navigation li li ul li').css('border', '0');
	jQuery('#navigation li.prnt').hover(function(){
		jQuery(this).find('ul:first').stop().css({overflow:'hidden', height:'auto', display:'none'}).slideDown(300, function(){
			jQuery(this).css({overflow:'visible', height:'auto'});
		});
	}, function(){
		jQuery(this).find('ul:first').stop().delay(300).slideUp(300, function(){
			jQuery(this).css({overflow:'hidden', display:'none'});
		});
	}); 
	jQuery('#navigation li.prnt li.prnt2').hover(function(){
		jQuery(this).find('ul:first').stop().css({overflow:'hidden', height:'auto', display:'none'}).slideDown(300, function(){
			jQuery(this).css({overflow:'visible', height:'auto'});
		});
	}, function(){
		jQuery(this).find('ul:first').stop().delay(300).slideUp(300, function(){
			jQuery(this).css({overflow:'hidden', display:'none'});
		});
	}); 
});



/* Home Widgets Separator */
jQuery(document).ready(function(){
	var $fheight = jQuery('#home .widget:eq(0)').height();
	var $sheight = jQuery('#home .widget:eq(1)').height();
	var $theight = jQuery('#home .widget:eq(2)').height();
	if( $fheight > $sheight && $fheight > $theight ){
		jQuery('#home .widget').css('height', $fheight);
	}else if( $sheight > $fheight && $sheight > $theight ){
		jQuery('#home .widget').css('min-height', $sheight);
	}else{
		jQuery('#home .widget').css('min-height', $theight);
	}
});



/* Bottom Widgets Separator */
jQuery(document).ready(function(){
	var $fheight = jQuery('#bottom .widget:eq(0)').height();
	var $sheight = jQuery('#bottom .widget:eq(1)').height();
	var $theight = jQuery('#bottom .widget:eq(2)').height();
	if( $fheight > $sheight && $fheight > $theight ){
		jQuery('#bottom .widget').css('height', $fheight);
	}else if( $sheight > $fheight && $sheight > $theight ){
		jQuery('#bottom .widget').css('min-height', $sheight);
	}else{
		jQuery('#bottom .widget').css('min-height', $theight);
	}
});



/* Form */
function submitform() {
    document.forms["commentform"].submit();
	return false;
};



/* Flickr Lightbox */
jQuery(document).ready(function() {
	jQuery("#flickr .flickr_badge_image a, .cmsmasters_flickr_widget .flickr_badge_image 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: 'prettyPhoto[flickr_gal]'
		});
	});
});



/* Pretty Photo Lighbox */
/* <![CDATA[ */
	jQuery(document).ready(function($) {
	  jQuery("a[rel^='prettyPhoto']").prettyPhoto({
		animationSpeed: 'normal',
		padding: 20,
		opacity: 0.65,
		showTitle: true,
		allowresize: true,
		counter_separator_label: ' / ',
		theme: 'light_square',
		wmode: 'opaque',
		autoplay: 1,
		hideflash: false,
		modal: false,
		changepicturecallback: function(){},
		callback: function(){},
		markup: '<div class="pp_pic_holder"> \
			  <div class="pp_top"> \
				<div class="pp_left"></div> \
				<div class="pp_middle"></div> \
				<div class="pp_right"></div> \
			  </div> \
			  <div class="pp_content_container"> \
				<div class="pp_left"> \
				<div class="pp_right"> \
				  <div class="pp_content"> \
					<div class="pp_fade"> \
					  <a href="#" class="pp_expand" title="Expand the image">Expand</a> \
					  <div class="pp_loaderIcon"></div> \
					  <div class="pp_hoverContainer"> \
						<a class="pp_next" href="#">next</a> \
						<a class="pp_previous" href="#">previous</a> \
					  </div> \
					  <div id="pp_full_res"></div> \
					  <div class="pp_details clearfix"> \
						<p class="pp_description"></p> \
						<span class="pp_close">Press Esc or <a href="#">Close</a></span> \
						<div class="pp_nav"> \
						  <a href="#" class="pp_arrow_previous">Previous</a> \
						  <p class="currentTextHolder">0/0</p> \
						  <a href="#" class="pp_arrow_next">Next</a> \
						</div> \
					  </div> \
					</div> \
				  </div> \
				</div> \
				</div> \
			  </div> \
			  <div class="pp_bottom"> \
				<div class="pp_left"></div> \
				<div class="pp_middle"></div> \
				<div class="pp_right"></div> \
			  </div> \
			</div> \
			<div class="pp_overlay"></div> \
			<div class="ppt"></div>',
		image_markup: '<img id="fullResImage" src="" />',
		flash_markup: '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="{width}" height="{height}"><param name="wmode" value="{wmode}" /><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="{path}" /><embed src="{path}" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="{width}" height="{height}" wmode="{wmode}"></embed></object>',
		quicktime_markup: '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab" height="{height}" width="{width}"><param name="src" value="{path}"><param name="autoplay" value="{autoplay}"><param name="type" value="video/quicktime"><embed src="{path}" height="{height}" width="{width}" autoplay="{autoplay}" type="video/quicktime" pluginspage="http://www.apple.com/quicktime/download/"></embed></object>',
		iframe_markup: '<iframe src ="{path}" width="{width}" height="{height}" frameborder="no"></iframe>',
		inline_markup: '<div class="pp_inline clearfix">{content}</div>'
	  });
	});
/* ]]> */



/* Scroll Top */
jQuery(document).ready(function(){
	jQuery('.divider a').click(function(){
		jQuery('html, body').animate({scrollTop:0}, 'slow');
		return false;
	});
});



/* Toggle */
jQuery(document).ready(function(){
	jQuery('.togg .tog a').click(function(e){
		var dropDown = jQuery(this).parent().next();
		jQuery(this).parent().parent().find('.tab_content').not(dropDown).slideUp();
		if ( jQuery(this).parent().hasClass('current') ) {
			jQuery(this).parent().removeClass('current');
		} else { 
			jQuery(this).parent().addClass('current');
		}
		dropDown.stop(false,true).slideToggle();
		e.preventDefault();
	})
});



/* Accordion */
jQuery(document).ready(function(){
	jQuery('.accordion .tog a').click(function(e){
		if ( jQuery(this).parent().hasClass('current') ) { 
			jQuery(this).parent().removeClass('current');
		} else { 
			jQuery(this).parent().parent().parent().find('.tog').removeClass('current');
			jQuery(this).parent().addClass('current');
		}
		var dropDown = jQuery(this).parent().next();
		jQuery(this).parent().parent().parent().find('.tab_content').not(dropDown).slideUp();
		dropDown.stop(false,true).slideToggle();
		e.preventDefault();
	})
});



/* 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();
	})
});



/* Lightbox Image Hover */
jQuery(document).ready(function(){
	var $hov = jQuery('#middle a img').not('.wp-pagenavi a img').not('.product a img').not('.pr_thumb a img').parent();
	jQuery.each($hov, function(){
		var $rel = jQuery(this).attr('rel');
		if($rel != ''){ 
			jQuery(this).addClass('hov');
			$imgh = jQuery(this).find('img').height();
			$imgw = jQuery(this).find('img').width();
			jQuery(this).prepend('<span></span>');
			jQuery(this).find('span').css({ width: $imgw, height: $imgh, margin: '5px' });
			var $hovs = jQuery(this).find('span');
			$hovs.css('opacity', '0');
			jQuery(this).hover(
				function(){
					$hovs.stop().animate({ opacity: '0.3' }, 500);
				},
				function(){
					$hovs.stop().animate({ opacity: '0' }, 500);
				}
			);
		}
	});
});



/* Remove Navigation Hover */
jQuery(document).ready(function(){
	jQuery('.navigation a.hov').removeClass('hov').find('span').css('margin', '0');
});



/* Portfolio Filter */
jQuery(document).ready(function(){
	var newFilter = '';
	jQuery('.p_filter li:first-child').addClass('current');
	jQuery('.p_filter li a').click(function(){
		newFilter = jQuery(this).attr('name');
		jQuery('.portfolio').fadeTo(500, 0.25);
		jQuery('.p_filter li').removeClass('current').css('display', 'block');
		jQuery(this).parent().addClass("current");
		jQuery('.p_item').not('.'+newFilter).slideUp();
		jQuery('.p_item').not('.gallery').find('a').find('img').parent().not(jQuery(this).attr({rel: 'prettyPhoto[portfolio]'})).attr({rel: 'prettyPhoto[portfolio]'});
		jQuery('.'+newFilter).not('.gallery').find('a').find('img').parent().attr({rel: 'prettyPhoto[portfolio'+newFilter+']'});
		jQuery('.'+newFilter).slideDown();
		jQuery('.portfolio').fadeTo(500, 1);
		return false;
	});
	jQuery('.p_filter').hover(function(){
		jQuery(this).find('li').slideDown(250);
	}, function(){
		jQuery(this).find('li').not('.current').slideUp(250);
	});
});



/* Portfolio Sort */
jQuery.fn.sort = (function(){
	var sort = [].sort;
	return function(comparator, getSortable){
		getSortable = getSortable || function(){return this;};
		var placements = this.map(function(){
			var sortElement = getSortable.call(this),
			parentNode = sortElement.parentNode,
			nextSibling = parentNode.insertBefore(
				document.createTextNode(''),
				sortElement.nextSibling
			);
			return function(){
				if (parentNode === this){
					throw new Error( "You can't sort elements if any one is a descendant of another." );
				}
				parentNode.insertBefore(this, nextSibling);
				parentNode.removeChild(nextSibling);
			};
		});
		return sort.call(this, comparator).each(function(i){
			placements[i].call(getSortable.call(this));
		});
	};
})();

jQuery(document).ready(function(){
	var $date = jQuery('.p_sort a[name="date"]'), $name = jQuery('.p_sort a[name="name"]'), inverse = false;
	$name.click(function(){
		jQuery('.portfolio').fadeTo(100, 0.25).delay(300).fadeTo(100, 1);
		$p_item = jQuery('.portfolio .p_item');
		$p_item.sort(function(a, b){
			a = jQuery(a).find('h5').text();
			b = jQuery(b).find('h5').text();
			return ( isNaN(a) || isNaN(b) ? a > b : +a > +b ) ? inverse ? -1 : 1 : inverse ? 1 : -1;
		});
		$date.removeClass('sel').removeClass('sel2');
		if(jQuery(this).hasClass('sel')){
			jQuery(this).removeClass('sel').addClass('sel2');
		}else{
			jQuery(this).removeClass('sel2').addClass('sel');
		}
		inverse = !inverse;
		return false;
	});
	$date.click(function(){
		jQuery('.portfolio').fadeTo(100, 0.25).delay(300).fadeTo(100, 1);
		$p_item = jQuery('.portfolio .p_item');
		$p_item.sort(function(a, b){
			a = jQuery(a).find('.p_date').text();
			b = jQuery(b).find('.p_date').text();
			return ( isNaN(a) || isNaN(b) ? a > b : +a > +b ) ? inverse ? -1 : 1 : inverse ? 1 : -1;
		});
		$name.removeClass('sel').removeClass('sel2');
		if(jQuery(this).hasClass('sel')){
			jQuery(this).removeClass('sel').addClass('sel2');
		}else{
			jQuery(this).removeClass('sel2').addClass('sel');
		}
		inverse = !inverse;
		return false;
	});
});



/* Image Preloader  */
jQuery(function () {
	var $imgContainerClass = ".p_item";
	var $images = jQuery($imgContainerClass+' img');
	var $max = $images.length;
	$images.remove();
	if($max>0) {
		LoadImage(0,$max);
	}
	function LoadImage(index,$max) {
		if(index<$max) {
			jQuery('<div id="img'+(index+1)+'"></div>').each(function() {
			   jQuery(this).appendTo(jQuery('.p_item a.preloader').eq(index));
			});
			var $img = new Image();
			var $curr = jQuery("#img"+(index+1));
			jQuery($img).load(function () {
				jQuery(this).css('display','none');
				jQuery($curr).append(this);
				jQuery(this).fadeIn('slow',function() {
					jQuery(this).parent().css("background-image", "none");
					if(index == ($max-1)) {
					}else{
						LoadImage(index+1,$max);
					}
				});
			}).error(function () {
				jQuery($curr).remove();
				LoadImage(index+1,$max);
			}).attr('src', jQuery($images[index]).attr('src')).attr('class', jQuery($images[index]).attr('class')).attr('title', jQuery($images[index]).attr('title')).attr('alt', jQuery($images[index]).attr('alt'));
		}
	}
});



/* Project Slider */
jQuery('#folio_slides .folio_slide').click(function(){
	$imgwidth = jQuery('img#folio_slide').width() + 10;
	$imgheight = jQuery('img#folio_slide').height() + 12;
	jQuery('img#folio_slide').parent().css({width: $imgwidth, minHeight: $imgheight});
	$href = jQuery(this).attr('href');
	jQuery('img#folio_slide').fadeOut(500, function(){
		jQuery('img#folio_slide').next().append('<img src="'+$href+'" alt="" />');
		$imgload = jQuery('#folio_slides div.dn img');
		jQuery($imgload).load(function(){
			jQuery('img#folio_slide').parent().css({width: 0, minHeight: 0});
			jQuery('img#folio_slide').attr('src', $href);
			jQuery('img#folio_slide').fadeIn(1000);
			jQuery(this).remove();
		});
	});
	return false;
});



/* Contact Form Validation */
function checkemail(emailaddress){
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i); 
	return pattern.test(emailaddress); 
}

