// JavaScript Document


$(document).ready(
	function() {
		
		$(".navigation li").hover(function(){
			$(this).addClass("hover");
			},function(){
			$(this).removeClass("hover");
		});
		
		$(".ico a").hover(function(){
			$(this).children("img").css('margin-top', '-12px');
			},function(){
			$(this).children("img").css('margin-top', '0');
		});
		
		$(".sidenav span.with-children").click(function(){
			$(this).next('h4').next("ul").slideToggle("slow")
			if($(this).hasClass('open') || $(this).hasClass('close')) $(this).toggleClass("open");
			if($(this).hasClass('open_orange') || $(this).hasClass('close_orange')) $(this).toggleClass("open_orange");
			$(this).siblings(".open").addClass("close");
		});
		
		$(".news").hover(function(){
			$(this).children(".news_top").addClass("hover");
			},function(){
			$(this).children(".news_top").removeClass("hover");
		});
		
		$(".nav_head").hover(function(){
			$(this).children(".nav_body").show();
			},function(){
			$(this).children(".nav_body").hide();
		});
		
		$(".darker").height($(document).height());
		
		$(".arrows").click( function() { 
			$(".center_big").hide("slow");
			$(".center").show("slow");
		} );
		
		$(".basket .close").click(function(){
			$(this).parents(".item").animate({ opacity: 'hide' }, "slow");
		});
		
		$(".table tr:nth-child(even)").addClass("chet");
		
		$(".expanded_link").toggle(function(){
			$(this).text("");
			$(this).addClass("close");
			$(".expanded_search").slideDown();
			},function(){
			$(this).text(" ");
			$(this).removeClass("close");
			$(".expanded_search").slideUp();
		});
		
		$(".photo_gallery a").hover(function(){
			$(this).parents(".item").addClass("hover");
			},function(){
			$(this).parents(".item").removeClass("hover");
		});
		
		$("a.mediaplayer-video[rel]").overlay({
			// use the Apple effect for overlay
			expose: '#789',
			top: 'center',
			onLoad: function(content) {
				// find the player contained inside this overlay and load it
				var video_id = this.getOverlay().find("a.player-video").attr('id').substr(6);
				$.ajax({
					type: "POST",
					data: 'id=' + video_id,
					url: base_url + 'videos/addview/' + video_id
				});
				var videoplayer = this.getOverlay().find("a.player-video").flowplayer(0);
				
				videoplayer.onStart(function(clip) {
		        	var wrap = jQuery(this.getParent());
					var p = clip.metaData.width / clip.metaData.height;

					var width = wrap.width();
					var height = wrap.height();
					
					if(clip.metaData.width > width) {
						height = width / p;
					}
					else {
						height = clip.metaData.height;
						width = clip.metaData.width;
					}
		
		        	wrap.css({width: width, height: height});
		    	});
				videoplayer.load();

			},
			onClose: function(content) {
				$f().unload();
			}
		});				
		// install flowplayers
		$("a.player-video").flowplayer("http://www.sjoptions.com/assets/flowplayer/flowplayer-3.2.7.swf"); 

		$("a.mediaplayer-audio[rel]").overlay({
			// use the Apple effect for overlay
			expose: '#789',
			top: 'center',
			onLoad: function(content) {
				var audio = this.getOverlay().find("a.player-audio");
				if(audio.hasClass('audio-count-views')) {
					var audio_id = audio.attr('id').substr(6);
					$.ajax({
						type: "POST",
						data: 'id=' + audio_id,
						url: base_url + 'audios/addview/' + audio_id
					});					
				}
				this.getOverlay().find("a.player-audio").flowplayer(0).load();
			},
			onClose: function(content) {
				$f().unload();
			}
		});				
		// install flowplayers
		$("a.player-audio").flowplayer("http://www.sjoptions.com/assets/flowplayer/flowplayer-3.2.5.swf", {
	        plugins: {
	            controls: {
	                fullscreen: false,
	                height: 30,
	                autoHide: false
	            }
	        },
	        clip: {
	            autoPlay: true
	        }
	    });

		$('.module_content_video_inner a object').remove();
		$('.module_content_video_inner a').each(function(){
			var container = $(this).parent();
			var player_id = $(this).attr('id');
			flowplayer(player_id, "http://www.sjoptions.com/assets/flowplayer/flowplayer-3.2.7.swf", {
				clip: {
					scaling: 'fit',
			    	onStart: function(clip) {
			        	var wrap = jQuery(this.getParent());
						var p = clip.metaData.width / clip.metaData.height;

						var width = wrap.width();
						var height = wrap.height();
						
						if(clip.metaData.width > width) {
							height = width / p;
						}
						else {
							height = clip.metaData.height;
							width = clip.metaData.width;
						}
			
			        	wrap.css({width: width, height: height});
						container.css({width: width, height: height});
			    	}
			    }
			});
		});

		$('.module_content_audio_inner a object').remove();
		$('.module_content_audio_inner a').each(function(){
			var player_id = $(this).attr('id');
			flowplayer(player_id, "http://www.sjoptions.com/assets/flowplayer/flowplayer-3.2.5.swf", {
		    	clip: {autoPlay: false, autoBuffering: false}});
		});
		
		var video_id = gup('video_id');
		if(video_id) {
			$('a[rel="#overlay'+ video_id +'"]').click();
		};
		
		var current_url = window.location.href;
		var parts = current_url.split('/');
		var l=parts.length;
		if(parts[l-2]=='videos') {
			video_url = parts[l-1];
			$.get('http://www.sjoptions.com/videos/get_video_id/' + video_url, function(video_id) {
				if (video_id>0) {
					$('a[rel="#overlay'+ video_id +'"]').click();
				}
			});
		};
		
});

function gup(name)
{
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec(window.location.href);
	if( results == null )
		return "";
	else
		return results[1];
}

function email_is_valid(string) {
  return (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1);
};

function check_contact_form() {
	var valid = email_is_valid($('#contact_youremail').val());
	if(!valid) alert('Inserted email is not valid');
	
	var msg = $('TEXTAREA[name="contact_message"]').val();
	if(msg.search(/(\S+[\/\.]){2,}/) != -1){
		alert('Message contains url');
		valid = false;
	}
	
	return valid;
};

function check_request_form() {
	var valid = email_is_valid($('#request_email').val());
	if(!valid) alert('Inserted email is not valid');
	return valid;
};

function submitPopup(node){
	var email = $(node).find("INPUT[name='email']").val();
	//console.log("Submitted: "+email);
	//console.log("Valid: "+email_is_valid(email));
	if(email_is_valid(email)) $.cookie('newsletter', email, { expires: 365 });
	
}

function getNamed(node){
	var msg={};
	$(node).find("[name]").each(function(){
		msg[$(this).attr("name")] = $(this).val();
	});	
	return msg;
}

//function setCookie(cookie_name,cookie_val){
//    $.cookie(cookie_name, cookie_val, {
//         expires: 365,
//         path: "/",
//         domain: "sjoptions.com",
//         secure: true
//    });
//}



function get_cookies_array() {

    var cookies = { };

    if (document.cookie && document.cookie != '') {
        var split = document.cookie.split(';');
        for (var i = 0; i < split.length; i++) {
            var name_value = split[i].split("=");
            name_value[0] = name_value[0].replace(/^ /, '');
            cookies[decodeURIComponent(name_value[0])] = decodeURIComponent(name_value[1]);
        }
    }

    return cookies;  
}

function printCookies(){
	var cookies = get_cookies_array();
	for(var name in cookies) {
	  console.log( name + " : " + cookies[name]);
	}
}

