//-----------------------------------------------------//
//                 ON READY ACTIONS                    //
//-----------------------------------------------------//

jQuery(document).ready(function (){
	
	// top links
	jQuery(".toplink").click(function(){
		jQuery(window.opera ? 'html' : 'html, body').animate({scrollTop: 0} , 1200 , "easeInOutCubic");
		return false;
	});
	
	 VideoJS.setupAllWhenReady();
	
	// swf
	jQuery(".swfobject").each(function(){
		jQuery(this).flash({
			swf: jQuery(this).attr("data-src") ,
			allowFullScreen: jQuery(this).attr("data-allowFullScreen") ,
			loop: jQuery(this).attr("data-loop") ,
			quality: jQuery(this).attr("data-quality") ,
			scale: jQuery(this).attr("data-scale") ,
			bgcolor: jQuery(this).attr("data-bgcolor") ,
			menu: jQuery(this).attr("data-menu") ,
			wmode: jQuery(this).attr("data-wmode") ,
			width: jQuery(this).attr("data-width") ,
			height: jQuery(this).attr("data-height") ,
			hasVersion: jQuery(this).attr("data-required-version") ,
			expressInstaller: jQuery(this).attr("data-expressInstaller")
		});
	});
	
	
	// j25 slider
	jQuery(".j25slider").each(function(){
		var $j25width = jQuery(this).attr("data-width");
		var $j25height = jQuery(this).attr("data-height");
		var $j25source = jQuery(this).attr("data-source");
		
		jQuery(this).flash({
			swf: template_path + "/utils/j25slider.swf?config=" + escape($j25source) ,
			allowFullScreen: true ,
			wmode: "transparent" ,
			width: $j25width ,
			height: $j25height,
			hasVersion: "10.0" ,
			expressInstaller: template_path + "/utils/expressinstall.swf"
		});
	});
	
	
	// nivo slider
	jQuery(".nivoSlider").each(function(){
		var $effect = jQuery(this).attr("data-effect");
		var $slices = jQuery(this).attr("data-slices");
		var $pauseOnHover = jQuery(this).attr("data-pauseOnHover");
		var $pauseTime = jQuery(this).attr("data-pauseTime");
		var $animSpeed = jQuery(this).attr("data-animSpeed");
		var $directionNav = jQuery(this).attr("data-directionNav");
		var $controlNav = jQuery(this).attr("data-controlNav");
		var $boxCols = jQuery(this).attr("data-boxCols");
		var $boxRows = jQuery(this).attr("data-boxRows");
		
		jQuery(this).nivoSlider({
			effect: $effect ,
			slices: Number($slices) ,
			pauseOnHover: $pauseOnHover ,
			pauseTime: $pauseTime ,
			animSpeed: $animSpeed ,
			controlNav: $controlNav == "1" ,
			boxCols: Number($boxCols) ,
			boxRows: Number($boxRows) ,
			directionNav: !($directionNav == "hide") ,
			directionNavHide: false
		});
		
		if($directionNav == "hover"){
			jQuery(this).children().find(".nivo-nextNav").hide();
			jQuery(this).children().find(".nivo-prevNav").hide();
			jQuery(this).hover(
			function(){
				jQuery(this).children().find(".nivo-nextNav").stop(true , true).fadeIn(150);
				jQuery(this).children().find(".nivo-prevNav").stop(true , true).fadeIn(150);
			}
			,
			function(){
				jQuery(this).children().find(".nivo-nextNav").stop(true , true).fadeOut(150);
				jQuery(this).children().find(".nivo-prevNav").stop(true , true).fadeOut(150);
			});
		}
	});
	
	
	// toggle content
	jQuery(".toggle_trigger").each(function(){
		jQuery(this).next().css("height", jQuery(this).next().height());
		jQuery(this).next().hide();
		jQuery(this).parent().removeClass("active");
		jQuery(this).click(function(){
				jQuery(this).next().slideToggle(Math.min(600 , Math.max(300 , jQuery(this).next().height())));
				jQuery(this).parent().toggleClass("active");
		});
	});
	
	
	// accordions
	jQuery(".accordion").each(function(){
		jQuery(this).children(".accordion_trigger").next().css("height", jQuery(this).children(".accordion_trigger").next().height());
	});
	jQuery(".accordion_trigger").next().hide();
	jQuery(".accordion_trigger").parent().removeClass("active");
	jQuery('.accordions').each(function(){
		jQuery(this).children('.accordion:first').children('.accordion_trigger').next().show().parent().toggleClass('active');
	});

	jQuery('.accordion_trigger').click(function(){
		if(jQuery(this).next().is(':hidden')){
			jQuery(this).parent().parent().children('.accordion').children('.accordion_trigger').next().slideUp(Math.min(600 , Math.max(300 , jQuery(this).next().height()))).parent().removeClass('active');
			jQuery(this).next().slideDown(Math.min(600 , Math.max(300 , jQuery(this).next().height()))).parent().toggleClass('active');
		}
	});
	
	
	// tabs
	jQuery(".tab_content").hide();
	jQuery('.tabs_wrap').each(function(){
		jQuery(this).children('.tabs').children('.tab:first').addClass('active');
		jQuery(this).children('.tabs_content').children('.tab_content:first').show();
	});
	
	jQuery('.tab').click(function(){
		if(!jQuery(this).hasClass('active')){
			jQuery(this).parent().children('.tab').removeClass("active");
			jQuery(this).addClass("active");
			jQuery(this).parent().parent().children('.tabs_content').children('.tab_content').hide();
			jQuery(this).parent().parent().children('.tabs_content').children('.tab_content:nth-child(' + (jQuery(this).index() + 1) + ')').fadeIn();
		}
	});
	
	
	// images
	jQuery(".image_overlay").each(function(){
		jQuery(this).parent().hover(
		function(){
			var ie = getInternetExplorerVersion();
			if(ie > 0 && ie < 9){
				jQuery(this).children(".image_overlay").stop(true , true).fadeTo(300 , 0.3);
				jQuery(this).children(".image_overlay").next().show();
			}else{
				jQuery(this).children(".image_overlay").stop(true , true).fadeTo(300 , 0.3);
				jQuery(this).children(".image_overlay").next().stop(true , true).fadeTo(300 , 1);
			}
		}
		
		,

		function(){
			var ie = getInternetExplorerVersion();
			if(ie > 0 && ie < 9){
				jQuery(this).children(".image_overlay").stop(true , true).fadeTo(300 , 0);
				jQuery(this).children(".image_overlay").next().hide();
			}else{
				jQuery(this).children(".image_overlay").stop(true , true).fadeTo(300 , 0);
				jQuery(this).children(".image_overlay").next().stop(true , true).fadeTo(300 , 0);
			}
		});
	});
	
	
	// buttons
	jQuery(".button_sc").each(function(){
		if(jQuery(this).parent().attr('data-color')){
			jQuery(this).css('color' , '#' + jQuery(this).parent().attr('data-color'));
		}
		
		if(jQuery(this).parent().attr('data-bgcolor')){
			jQuery(this).css('background-color' , '#' + jQuery(this).parent().attr('data-bgcolor'));
		}
	});
	
	jQuery(".button_sc").hover(
		function(){
			if(jQuery(this).parent().attr('data-hovercolor')){
				jQuery(this).css('color' , '#' + jQuery(this).parent().attr('data-hovercolor'));
			}
			
			if(jQuery(this).parent().attr('data-hoverbgcolor')){
				jQuery(this).css('background-color' , '#' + jQuery(this).parent().attr('data-hoverbgcolor'));
			}
		}
		
		,
		
		function(){
			if(jQuery(this).parent().attr('data-color')){
				jQuery(this).css('color' , '#' + jQuery(this).parent().attr('data-color'));
			}
			
			if(jQuery(this).parent().attr('data-bgcolor')){
				jQuery(this).css('background-color' , '#' + jQuery(this).parent().attr('data-bgcolor'));
			}
		}
	);
	
	jQuery(".button_sc").mousedown(function(){
		if(jQuery(this).parent().attr('data-activecolor')){
			jQuery(this).css('color' , '#' + jQuery(this).parent().attr('data-activecolor'));
		}
		
		if(jQuery(this).parent().attr('data-activebgcolor')){
			jQuery(this).css('background-color' , '#' + jQuery(this).parent().attr('data-activebgcolor'));
		}
	});
	
	jQuery(".button_sc").click(function(){
		if(jQuery(this).parent().attr('data-hovercolor')){
			jQuery(this).css('color' , '#' + jQuery(this).parent().attr('data-hovercolor'));
		}
		
		if(jQuery(this).parent().attr('data-hoverbgcolor')){
			jQuery(this).css('background-color' , '#' + jQuery(this).parent().attr('data-hoverbgcolor'));
		}
	});
	
	
	// social - widget
	jQuery(".social_widget_wrap").each(function(){
		jQuery(this).find("img").each(function(){
			jQuery(this).hover(
				function(){
				jQuery(this).stop(true , true).fadeTo(150 , 0.8);
				}
				,
				function(){
				jQuery(this).stop(true , true).fadeTo(150 , 1);
				}
			);
		});
	});
	
	
	// contact form - widget
	jQuery(".contact_form_widget_wrap").each(function(){
		var form = jQuery(this).children("form");
		
		form.children("button[type='submit']").click(function(){
			var name = form.children().find("input[name='name']").val();
			var email = form.children().find("input[name='email']").val();
			var message = form.children().find("textarea[name='message']").val();
			
			var dataString = "name=" + name + "&email=" + email + "&message=" + message + "&to=" + form.children().find("input[name='to']").val() + "&name_required=" + (form.children().find("input[name='name']").attr("required") ? "true" : "false") + "&email_required=" + (form.children().find("input[name='email']").attr("required") ? "true" : "false") + "&return=code";
			
			form.children().find("input[name='email']").removeClass("invalid");
			
			if(name == "" && form.children().find("input[name='name']").attr("required")){
				form.children().find("input[name='name']").focus();
				return false;
			}
			
			if((email == "" || email.indexOf("@",0) == -1 || email.indexOf(".",0) == -1) && form.children().find("input[name='email']").attr("required")){
				form.children().find("input[name='email']").focus();
				return false;
			}
			
			if(message == ""){
				form.children().find("textarea[name='message']").focus();
				return false;
			}
			
			jQuery.ajax({
				type: "POST" ,
				url: form.attr("action") ,
				data: dataString ,
				success: function(r){
					if(r == "error3"){
						form.children().find("input[name='email']").focus();
						form.children().find("input[name='email']").addClass("invalid");
					}else
					if(r == "success"){
						form.hide();
						form.next().fadeIn(500);
					}
				}
			});
			return false;
		});
	});
	
	
	// search form - widget
	jQuery(".search_form_widget_wrap").each(function(){
		jQuery(this).find("button[type='submit']").click(function(){
			if(jQuery(this).prev().val() == ""){
				jQuery(this).prev().focus();
				return false;
			}
		});
	});
	
	
	// twitter - widget
	jQuery(".twitter_widget_wrap").each(function(){
		var $username		= jQuery(this).attr('data-username');
		var $count 			= jQuery(this).attr('data-count');
		var $loadingText 	= jQuery(this).attr('data-ltext');
		var $interval 		= jQuery(this).attr('data-interval');
		
		jQuery(this).tweet({
            username: $username ,
            join_text: null ,
            avatar_size: null ,
            count: $count ,
			refresh_interval: Number($interval) , 
			loading_text: $loadingText
        });
		
		jQuery(this).bind('loaded', function(){
		  updateLayout();
		});
	});
	
	
	// flickr - widget
	jQuery('.flickr_widget_wrap').each(function(){
		var $user_id		= jQuery(this).attr('data-user_id');
		var $count 			= jQuery(this).attr('data-count');
		
		jQuery(this).jflickrfeed({
			limit: $count,
			qstrings: {
				id: $user_id
			},
			itemTemplate: '<a href="{{link}}" target = "_blank" title = "{{title}}" ><img src="{{image_s}}" alt="{{title}}" width = "75" height = "75" /></a>'
		}
		
		,
		
		function(data){
			jQuery(this).children(".loading_text").hide();
			updateLayout();
			jQuery(this).find("img").each(function(){
				jQuery(this).load(function(){
					updateLayout();
				});
			});
		});
	});
	
	
	// colorbox gallery
	jQuery(".colorbox_gallery").each(function(){
		jQuery(this).find(".gallery-icon").children("a").addClass("cboxElement colorbox");
		jQuery(this).find(".gallery-icon").children("a").attr("rel" , jQuery(this).children(".gallery").attr("id"));
	});
	
	
	// colorbox
	jQuery(".colorbox").each(function(){
		var $title			= jQuery(this).attr('data-title') || "";
		var $innerWidth		= jQuery(this).attr('data-width') || false;
		var $innerHeight 	= jQuery(this).attr('data-height') || false;
		var $slideshow 		= jQuery(this).attr('data-slideshow') || false;
		var $slideshowAuto 	= jQuery(this).attr('data-slideshowauto') || false;
		var $slideshowSpeed	= jQuery(this).attr('data-slideshowspeed') || 2500;
		var $iframe 		= jQuery(this).attr('data-iframe') || false;
		var $inline 		= jQuery(this).attr('data-inline') || false;
		var $html 			= jQuery(this).attr('data-html') || "";
		var $photo 			= jQuery(this).attr('data-photo') || false;
		var $opacity 		= jQuery(this).attr('data-opacity') || 75;
		
		$opacity = Number($opacity)/100;
		
		$slideshow 		= String($slideshow).toLowerCase() == "true" ? true : false;
		$slideshowAuto 	= String($slideshowAuto).toLowerCase() == "true" ? true : false;
		$iframe 		= String($iframe).toLowerCase() == "true" ? true : false;
		$inline 		= String($inline).toLowerCase() == "true" ? true : false;
		$photo  		= String($photo).toLowerCase() == "true" ? true : false;
		
		if(!$innerWidth){
			if($iframe == true){
				$innerWidth = 480;
			}else{
				$innerWidth = false;
			}
		}
		
		if(!$innerHeight){
			if($iframe == true){
				$innerHeight = 360;
			}else{
				$innerHeight = false;
			}
		}
		
		jQuery(this).colorbox({
								title:			$title ,
								maxWidth:		"100%" ,
								maxHeight:		"100%" ,
								speed:			1000 ,
								innerWidth: 	$innerWidth ,
								innerHeight: 	$innerHeight ,
								iframe: 		$iframe ,
								inline: 		$inline ,
								html: 			$html ,
								photo: 			$photo ,
								opacity: 		$opacity ,
								slideshow:		$slideshow ,
								slideshowAuto:	$slideshowAuto ,
								slideshowSpeed:	$slideshowSpeed ,
								current: 		"" ,
								onOpen: function(){
										jQuery("#cboxNext").hide();
										jQuery("#cboxPrevious").hide();
										jQuery("#cboxClose").hide();
										jQuery("#cboxSlideshow").hide();
										
										jQuery("#colorbox").hover(
											function(){
													if(!jQuery("#cboxContent").hasClass("cboxSingle")){
														jQuery("#cboxNext").fadeIn(150);
														jQuery("#cboxPrevious").fadeIn(150);
													}
													if(jQuery("#cboxContent").hasClass("cboxSlideshow")){
														jQuery("#cboxSlideshow").fadeIn(150);
													}
													jQuery("#cboxClose").fadeIn(150);
											}
											,
											function(){
													if(!jQuery("#cboxContent").hasClass("cboxSingle")){
														jQuery("#cboxNext").fadeOut(150);
														jQuery("#cboxPrevious").fadeOut(150);
													}
													if(jQuery("#cboxContent").hasClass("cboxSlideshow")){
														jQuery("#cboxSlideshow").fadeOut(150);
													}
													jQuery("#cboxClose").fadeOut(150);
											}
										);
								}//end onOpen
								,
								onCleanup: function(){
												jQuery("#cboxContent").removeClass("cboxSingle");
												jQuery("#cboxContent").removeClass("cboxSlideshow");
												
								}//end onCleanup
									});
	});
	
	
	
	
	updateLayout();
});




//-----------------------------------------------------//
//                    FUNCTIONS                        //
//-----------------------------------------------------//

function updateLayout(){
	jQuery("#page").css("padding-bottom" , jQuery("footer").outerHeight());
}

function getInternetExplorerVersion(){
	var rv = -1;
	if(navigator.appName == 'Microsoft Internet Explorer'){
		var ua = navigator.userAgent;
		var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
		if(re.exec(ua) != null){
			rv = parseFloat(RegExp.$1);
		}
	}
	return rv;
}

// google maps
function initGMap(mapID , options){
	if(options.address && options.address.length > 0){
		var geocoder = new google.maps.Geocoder();
		geocoder.geocode({'address': options.address} , function(result , status){
																	options.latitude = result[0].geometry.location.lat();
																	options.longitude = result[0].geometry.location.lng();
																	GMap(mapID , options);
														});
	}else{
		GMap(mapID , options);
	}
}

function GMap(mapID , options){
	var mapOptions = {
			  mapTypeId: options.type ,
			  zoom: Number(options.zoom) ,
			  navigationControl: options.nav_controls == "true" ? true : false ,
			  streetViewControl: options.enable_street_view == "true" ? true : false ,
			  mapTypeControl: options.map_type_controls == "true" ? true : false ,
			  draggable: options.draggable == "true" ? true : false ,
			  scrollwheel: options.scrollwheel == "true" ? true : false ,
			  center: new google.maps.LatLng(options.latitude , options.longitude)
			};
	
	var map = new google.maps.Map(document.getElementById(mapID) , mapOptions);
	
	for(var i = 0 ; i < options.markers.length ; i++){
		initGMarker(map , options.markers[i]);
	}
}

function initGMarker(map , markerOptions){
	if(markerOptions.address && markerOptions.address.length > 0){
		var geocoder = new google.maps.Geocoder();
		geocoder.geocode({'address': markerOptions.address} , function(result , status){
																	markerOptions.latitude = result[0].geometry.location.lat();
																	markerOptions.longitude = result[0].geometry.location.lng();
																	GMarker(map , markerOptions);
														});
	}else{
		GMarker(map , markerOptions);
	}
}

function GMarker(map , markerOptions){
	var markerShadow = new google.maps.MarkerImage(template_path + '/images/maps/shadow.png' ,
												 new google.maps.Size(59, 32) ,
												 new google.maps.Point(0,0) ,
												 new google.maps.Point(16, 32));
												 
	var markerIcon = new google.maps.MarkerImage(template_path + "/images/maps/" + markerOptions.color + "-dot.png",
												 new google.maps.Size(32, 32) ,
												 new google.maps.Point(0,0) ,
												 new google.maps.Point(16, 32));
														 
	var marker = new google.maps.Marker({
		map: map ,
		draggable:false ,
		shadow: markerShadow ,
		icon: markerIcon ,
		title: markerOptions.title ,
		position:  new google.maps.LatLng(markerOptions.latitude , markerOptions.longitude),
	});
}
