var slideShowSpeed = 1500;
var slideEffect = 'fade';
var slideShowTimeout = 5000;

/*****************************************************************************************/
/************************** No need to modify code after this line ***********************/
/*****************************************************************************************/


var $j = jQuery.noConflict();

function portfolioHover() {
	$j('.h_portfolio_th, .h_portfolio_th_half, .teaser_grid').hover(
		function () {
			//mouse hover
			//|| $j(this).closest('.first_section').hasClass('portfolio_teasers')
			if ($j(this).closest('.posts_grid').hasClass('thumbnail_title')
				|| ($j(this).closest('.posts_grid').hasClass('title_thumbnail_text') && $j(this).closest('.posts_grid').hasClass('columns_count_1_thumbnail_title_text') == false )
				|| $j(this).closest('.posts_grid').hasClass('h_portfolio_grid') ) {
				$j(this).find('h3').stop().animate({"opacity" : 0}, 500, function() { $j(this).css({"display" : "none"}); });
			}
			$j(this).find('img').stop().animate({"left" : -50}, 500);
		}, 
		function () {
			//mouse out
			//|| $j(this).closest('.first_section').hasClass('portfolio_teasers')
			if ( $j(this).closest('.posts_grid').hasClass('thumbnail_title')
				|| ($j(this).closest('.posts_grid').hasClass('title_thumbnail_text') && $j(this).closest('.posts_grid').hasClass('columns_count_1_thumbnail_title_text') == false )
				|| $j(this).closest('.posts_grid').hasClass('h_portfolio_grid') ) {
				$j(this).find('h3').stop().css({"display" : "inherit"}).animate({"opacity" : 1}, 500);
			}
			$j(this).find('img').stop().animate({"left" : 0}, 500);
		}
	);
}

function portfolioSortable() {
	jQuery('.wpb_sort li a.wpb_sortable_cats').click(function(e) {
		e.preventDefault();
		var classname = jQuery(this).attr('data-value');
		jQuery('.wpb_sortable_current').removeClass('wpb_sortable_current');
		jQuery(this).addClass('wpb_sortable_current');
		
		jQuery('.wpb_sortable').stop().animate({"opacity" : 1});
		jQuery('.wpb_sortable:not(.'+classname+')').stop().animate({"opacity" : 0.2});
	});
	
	jQuery('.wpb_sort li.wpb_all_cats a').click(function(e) {
		e.preventDefault();
		jQuery('.wpb_sortable_current').removeClass('wpb_sortable_current');
		jQuery(this).addClass('wpb_sortable_current');
		
		jQuery('.wpb_sortable').stop().animate({"opacity" : 1});
	});
}

function menuBehaviour() {
	//jQuery(".header_menu li div.submenu").css({display: "none"}); // Opera Fix
	jQuery(".header_menu").children('li').children('.sub-menu').css({"display": "none"}); // Opera Fix
	
	jQuery(".header_menu li a").removeAttr('title');
	jQuery(".header_menu").children('li').each(function() {
		var sublist = jQuery(this).children('.sub-menu');
		jQuery(this).hover(function() {
			jQuery(this).addClass('hover');
			
			if (sublist.length > 0) {
				jQuery(this).addClass('hover_triangle');	
			}

			sublist.stop().css( {height:"auto", overflow:"hidden", display:"block", opacity: 0} )
			.animate({
				opacity: 1
			}, 400, function() {
			// Animation complete.
			});
		},
		function() {
			jQuery(this).removeClass('hover hover_triangle');
			
			sublist.stop().animate({
				opacity: 0
			}, 400, function() {
				$j(this).css( {display:"none", overflow:"hidden"} );
			// Animation complete.
			});
		});
	});
} // end of menuBehaviour() function


function triangleOverlap() {
	var canvas_i = 0;
	var cwidth = $j(window).width();
	$j('.triangle_top').each(function() {
		if ($j.browser.msie == false && slanted_panels == "true") {
			if ($j('#canvas_'+canvas_i).length == 0) {
				$j(this).append('<canvas id="canvas_'+canvas_i+'" width="'+cwidth+'" height="85"></canvas>');
			} else {
				$j('#canvas_'+canvas_i).remove();
				$j(this).append('<canvas id="canvas_'+canvas_i+'" width="'+cwidth+'" height="85"></canvas>');
			}
			$j('#canvas_'+canvas_i).css({"position" : "absolute", "top" : -85, "z-index" : 10});
			
			var canvas = document.getElementById("canvas_"+canvas_i);
			if (canvas.getContext) {
				$j(this).css({"padding-bottom" : 65});
				
				var ctx = canvas.getContext("2d");
				var triangle_bg;
					triangle_bg = $j(this).css("background-color");
					triangle_image = $j(this).css("background-image").replace('url("', "").replace('")', "");
				
				if (triangle_bg == "transparent" || triangle_bg == 'rgba(0, 0, 0, 0)') {
					var tmpclass = $j(this).attr("class");
					tmpclass = tmpclass.split(" ");
					tmpclass = tmpclass[0].split("_");
					
					if ( tmpclass[0] == 'color' ) {
						triangle_bg = "#"+tmpclass[1];
						$j(this).css({"background-color" : triangle_bg});
					} else {
						triangle_bg = "no color found";
					}
				}
				
				if ( triangle_bg == "no color found" ) {
					triangle_bg = $j("body").css("background-color");
					
					var body_img = $j("body").css("background-image").replace('url("', "").replace('")', "").replace('url(', '')
.replace(')', '');
					if (body_img != 'none') {
						var p_img = new Image();  
						p_img.src = body_img;
						try {
							triangle_bg = ctx.createPattern(p_img, 'repeat');
						} catch (err) { }
					}
				}
				
				if (triangle_image != "none") {
					var p_img = new Image();  
					p_img.src = triangle_image;
					triangle_bg = ctx.createPattern(p_img, 'repeat');
				}
				
				ctx.clearRect(0,0, canvas.width, canvas.height);
				ctx.fillStyle = triangle_bg;
				ctx.beginPath();  
				ctx.moveTo(0,85);  
				ctx.lineTo(cwidth, 0);
				ctx.lineTo(cwidth, 85);  
				ctx.closePath();
				ctx.fill();	
			}
			canvas_i++;
		} else {
			//IE or slanted panels are switched off
			var triangle_bg = $j(this).css("background-color");
			if (triangle_bg == "transparent" || triangle_bg == 'rgba(0, 0, 0, 0)') {
				var tmpclass = $j(this).attr("class");
				tmpclass = tmpclass.split(" ");
				tmpclass = tmpclass[0].split("_");
				
				if ( tmpclass[0] == 'color' ) {
					triangle_bg = "#"+tmpclass[1];
					$j(this).css({"background-color" : triangle_bg});
				}
			}
		}
	});
}

function setupSliders() {
	/*$j('#homeslider .container .slide_txt h1.title').each(function(index) {
		var new_t = $j(this).html().replace("<br>", '</span><span>');
		$j(this).html(new_t);
	});*/
	
	$j('#homeslider .container')
	.addClass('wpb_home_cycle')
	.cycle({
		fx: slideEffect, // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		speed: slideShowSpeed,
		timeout: 100*1000,
		before: homeSlideshowOnBefore,
		after:homeSlideshowOnAfter,
		next: '.h_next',
		prev: '.h_prev'
	});
	$j('#homeslider .container').cycle("pause");
	
	jQuery(window).load(function() {
		$j('#homeslider .container').cycle("resume");
	});
} // end of setupSliders function

function homeSlideshowOnBefore(currSlideElement, nextSlideElement, options) {
	$j(nextSlideElement).find('h1 span').animate({"opacity" : 0, "left" : 200}, 0);
	$j(nextSlideElement).find('a.animate').animate({"opacity" : 0}, 0);
	//$j(nextSlideElement).find('p').animate({"opacity" : 0, "left" : 75}, 0);
}
function homeSlideshowOnAfter(currSlideElement, nextSlideElement, options) {
	//$j(nextSlideElement).find('h1').stop().animate({"opacity" : 1, "left" : 0}, 800);
	var dd = 100;
	$j(nextSlideElement).find('h1 span').each(function() {
		dd += 80;
		$j(this).stop().delay(parseInt(dd)).animate({"opacity" : 1, "left" : 0}, 800);
	});
	
	dd += 500;
	
	//$j(nextSlideElement).find('p').each(function() {
	$j(nextSlideElement).find('a.animate').each(function() {
		dd += 80;
		$j(this).stop().delay(parseInt(dd)).animate({"opacity" : 1}, 600);
	});
}

function homePortfolioThumbs() {
	$j('#homeslider .h_portfolio_grid .centering:eq(0)').css({"height" : "200px", "position" : "relative"});
	$j('#homeslider .h_portfolio_grid .h_portfolio_th').css({"position" : "absolute"});
	
	var th_w = $j('#homeslider .h_portfolio_grid .h_portfolio_th').width() + 20;
		left_1 = 0,
		left_2 = th_w,
		left_3 = th_w*2;
	
	$j('#homeslider .h_portfolio_grid .h_portfolio_th, .h_portfolio_controls, #homeslider .thumb_title').animate({"opacity" : 0}, 0);
	$j('#homeslider .h_portfolio_grid .h_portfolio_th').css({"left" : -300});
	
	$j('#homeslider .thumb_title').delay(1200).animate({"opacity" : 1}, 500);
	$j('#homeslider .h_portfolio_grid .h_portfolio_th').eq(0).css({"left" : left_1, "display" : "block"}).delay(1200).animate({"opacity" : 1}, 500);
	$j('#homeslider .h_portfolio_grid .h_portfolio_th').eq(1).css({"left" : left_2, "display" : "block"}).delay(1400).animate({"opacity" : 1}, 500);
	$j('#homeslider .h_portfolio_grid .h_portfolio_th').eq(2).css({"left" : left_3, "display" : "block"}).delay(1600).animate({"opacity" : 1}, 500);

	$j('#homeslider .h_portfolio_grid .h_portfolio_controls').delay(2500).animate({"opacity" : 1}, 500);
	
	var i = 0,
		aa, bb, cc, dd,
		th_total = $j('#homeslider .h_portfolio_grid .h_portfolio_th').length,
		z = th_total - 1,
		animation = false;
	
	$j('.h_next').click(function() {
		if (animation == false) {
			animation = true;
		
			aa = i;
			if ( aa < 0 ) { aa = th_total + aa; }
			
			bb = aa + 1;
			if ( bb < 0 ) { bb = th_total + bb; }
			else if ( bb >= th_total ) { bb = 0; }
			
			cc = bb + 1;
			if ( cc < 0 ) { cc = th_total + cc; }
			else if ( cc >= th_total ) { cc = 0; }
			
			dd = aa - 1;
			if ( dd < 0 ) { dd = th_total + dd; }
			else if ( dd >= th_total ) { dd = 0; }
					
			new_xx = ($j(window).width() - 1020)/2 + 1020 - $j('#homeslider .h_portfolio_grid .th_portfolio_eq_'+parseInt(cc)).outerWidth() -	parseInt($j('#homeslider .h_portfolio_grid .th_portfolio_eq_'+parseInt(cc)).css("margin-right").replace("px", ""));
			
			$j('#homeslider .h_portfolio_grid .th_portfolio_eq_'+parseInt(cc))
			.animate({
				"left" : new_xx,
				"opacity" : 0
			})
			.animate({"left" : -300}, 0, function() { $j(this).hide(); });
			
			$j('#homeslider .h_portfolio_grid .th_portfolio_eq_'+parseInt(bb))
			.delay(200)
			.animate({
				"left" : "+="+th_w+""
			});
			
			$j('#homeslider .h_portfolio_grid .th_portfolio_eq_'+parseInt(aa))
			.delay(300)
			.animate({
				"left" : "+="+th_w+""
			});
			
			$j('#homeslider .h_portfolio_grid .th_portfolio_eq_'+parseInt(dd))
			.delay(600)
			.show()
			.css({"left" : 0})
			.animate({
				"opacity" : 1
			},
			function() { animation = false; });
			
			i--;
			if (i <= -th_total) { i = 0; }
		}
		return false;
	});
	
	$j('.h_prev').click(function() {
		if (animation == false) {
			animation = true;
			aa = i;
			if ( aa < 0 ) { aa = th_total + aa; }
			
			bb = aa + 1;
			if ( bb < 0 ) { bb = th_total + bb; }
			else if ( bb >= th_total ) { bb = 0; }
			
			cc = bb + 1;
			if ( cc < 0 ) { cc = th_total + cc; }
			else if ( cc >= th_total ) { cc = 0; }
			
			dd = aa - 1;
			if ( dd < 0 ) { dd = th_total + dd; }
			else if ( dd >= th_total ) { dd = 0; }
					
			new_xx = - ($j(window).width()-1020)/2;
			
			$j('#homeslider .h_portfolio_grid .th_portfolio_eq_'+parseInt(aa))
			.animate({
				"left" : new_xx,
				"opacity" : 0
			},
			function() { $j(this).hide(); } );
			
			$j('#homeslider .h_portfolio_grid .th_portfolio_eq_'+parseInt(bb))
			.delay(200)
			.animate({
				"left" : "-="+th_w+""
			});
			
			$j('#homeslider .h_portfolio_grid .th_portfolio_eq_'+parseInt(cc))
			.delay(300)
			.animate({
				"left" : "-="+th_w+""
			});
			
			$j('#homeslider .h_portfolio_grid .th_portfolio_eq_'+parseInt(dd))
			.delay(600)
			.show()
			.css({"left" : left_3})
			.animate({
				"opacity" : 1
			},
			function() { animation = false; });
			
			i++;
			if (i >= th_total) { i = 0; }
		}
		return false;
	});
}


function inputBehaviour() {
	$j("input:text, textarea").each(function(){
		$j(this).attr({"rel": $j(this).val()});
		
		$j(this)
		.focus(function () {
			if ($j(this).val() == $j(this).attr('rel')) {
				$j(this).val('');
			}
			$j(this).addClass("focus");
		})
		.blur(function () {
			if ($j(this).val() == '') {
				$j(this).val($j(this).attr('rel'));
			} else {
				$j(this).addClass("notempty");
			}
			$j(this).removeClass("focus");
		});
	});
} // end of function inputBehaviour



function initJsSweetness() {
	$j("a[rel^='prettyPhoto'], a.prettyPhoto, .gallery a, #slider .prettyphoto a, a.prettyphoto").prettyPhoto({
		animationSpeed: 'normal', /* fast/slow/normal */
		padding: 15, /* padding for each side of the picture */
		opacity: 0.7, /* Value betwee 0 and 1 */
		showTitle: false, /* true/false */
		allowresize: true, /* true/false */
		counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
		theme: 'facebook', /* light_rounded / dark_rounded / light_square / dark_square */
		hideflash: false, /* Hides all the flash object on a page, set to TRUE if flash appears over prettyPhoto */
		modal: false, /* If set to true, only the close button will close the window */
		changepicturecallback: function(){}, /* Called everytime an item is shown/changed */
		callback: function(){} /* Called when prettyPhoto is closed */
	});
	
	//pricingTableBehaviour();
	
	menuBehaviour();
	setupSliders();
	
	inputBehaviour();
	triangleOverlap();
	
	homePortfolioThumbs();
	portfolioHover();
	portfolioSortable();
	//
	$j('.main_content table tr:even').addClass('even');
}

jQuery(document).ready(function($j) {
	initJsSweetness();
});

jQuery(window).resize(function() {
	triangleOverlap();
});

jQuery(window).load(function() {
	$j('.wpb_pageslider .pageslides_holder')
	.addClass('wpb_page_cycle')
	.cycle({
		fx: slideEffect, // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		speed: slideShowSpeed,
		timeout: 10*1000
	});
	
	triangleOverlap();
	portfolioHover();
});

/*function pricingTableBehaviour() {
	$j('.pricing_table .pricing_column').each(function(index) {
		$j(this).find('p:even').not('.heading').addClass('even');
		
		$j(this).find('p:last .large_button').parent().removeClass('even').addClass('tr_button');
		
		if ($j(this).next().hasClass('pricing_column_highlight')) {
			$j(this).find('p').each(function(index) {
				$j(this).not('.tr_button').css({
					"background-image"      : "url("+templatepath+"images/res/pricing_l_shadow.png)",
					"background-repeat"     : "repeat-y",
					"background-position"   : "top right"
				});
			});
		}
		
		if ($j(this).prev().hasClass('pricing_column_highlight')) {
			$j(this).find('p').each(function(index) {
				$j(this).not('.tr_button').css({
					"background-image"      : "url("+templatepath+"images/res/pricing_r_shadow.png)",
					"background-repeat"     : "repeat-y",
					"background-position"   : "top left"
				});
			});
		}
		
	});
	
	$j('.pricing_table').each(function(index) {
		var pr_table_width, column_count, column_width;
		pr_table_width = $j(this).width();
		column_count = $j(this).find('.pricing_column').length;
		
		column_width = pr_table_width / column_count;
		$j(this).find('.pricing_column').width(column_width);
	});
} // end of pricingTableBehaviour()
*/


(function($j) {
    var userAgent = navigator.userAgent.toLowerCase();

    $j.browser = {
        version: (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [0,'0'])[1],
        safari: /webkit/.test( userAgent ),
        opera: /opera/.test( userAgent ),
        msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
        mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
    };

})(jQuery);
