/**
 * javascript functions for lailaty website
 * Copyright (c) Enjaz Technology Inc (http://enjaz.net.sa)
 * developed by Mahmoud allam allam.mahmoud@hotmail.com
 *   
 */
 
  $(document).ready(function(){
	 
	
	 if($("a[rel='colorbox']").attr("href")!=null)
	 {
	 var vID = getParam('v',$("a[rel='colorbox']").attr("href"));
	// youtu.be link
	if(vID == ""){
		vID = $("a[rel='colorbox']").attr("href").split('youtu.be/');
		if(vID[1])
		{
			vID = movie_id[1];
			if(vID.indexOf('?') > 0)
				vID = movie_id.substr(0,vID.indexOf('?')); // Strip anything after the ?

			if(vID.indexOf('&') > 0)
				vID = vID.substr(0,vID.indexOf('&')); // Strip anything after the &
			
		}
		
		
	}
					if(vID)
					$("a[rel='colorbox']").attr("href",'http://www.youtube.com/embed/'+vID);
					
					
					
					
					
	 
	 $("a[rel='colorbox']").colorbox({iframe:true, innerWidth:425, innerHeight:344});
	 }
	 
	 
	 $("a[rel='colorBoxImg']").colorbox();
	 
	 
	 $("#productinfoimg a").click(function(){
			  
			  $("#galleryLoader").show();
			   //return false;
			   
			  if($(this).attr("rel")=="vid"){
			  // Regular youtube link
					movie_id = getParam('v',$(this).attr("href"));
					
					// youtu.be link
					if(movie_id == ""){
						movie_id = $(this).attr("href").split('youtu.be/');
						if(movie_id[1])
						{
							movie_id = movie_id[1];
							if(movie_id.indexOf('?') > 0)
								movie_id = movie_id.substr(0,movie_id.indexOf('?')); // Strip anything after the ?
	
							if(movie_id.indexOf('&') > 0)
								movie_id = movie_id.substr(0,movie_id.indexOf('&')); // Strip anything after the &
							
						}
						
						
					}
					if(movie_id)
					{
					movie = 'http://www.youtube.com/embed/'+movie_id;
					(getParam('rel',$(this).attr("href")))?movie+="?rel="+getParam('rel',$(this).attr("href")) : movie+="?rel=1";
					}
					var iframe='<iframe src ="{path}" width="450" height="320" frameborder="no"></iframe>';
					toInject = iframe.replace(/{path}/g,movie);
					$("#largeImg").html(toInject);
					$("#galleryLoader").hide();
					
			}
			  else
			  {	
			   var img = new Image();
			   var imgSRC=$(this).attr("href");
			   
			   $(img).load(function () {
				  var imgObj=this;
				  $linkObj=$("<a></a>").attr("rel","colorBoxImg").attr("href",imgSRC).html(imgObj);
				  $("#largeImg").html($linkObj);
				  $("a[rel='colorBoxImg']").colorbox();
					 
				  
				 $("#galleryLoader").hide();
				  $(this).fadeIn();

				}).error(function () {

				  $("#largeImg").html("حدث خطأ اثناء تحميل الصورة");
				  $("#galleryLoader").hide();

				}).attr('src',imgSRC);

			  }
				
				
			 
				
				
				
				return false;	
				
				});
	 
	$("#nlsubmit").click(function(){newsletters('add')});
	$("#nlremove").click(function(){newsletters('remove')});
	$(".catMenu").find("a.arrow").click(function(){$(this).parent().find("ul").toggle("fast");
	$(this).toggleClass("expand");
	});
 });



function getParam(name,url){
	  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	  var regexS = "[\\?&]"+name+"=([^&#]*)";
	  var regex = new RegExp( regexS );
	  var results = regex.exec( url );
	  return ( results == null ) ? "" : results[1];
	}
	
	
 
 function validateEmail(email) {

	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	if( !emailReg.test( email ) ) {
		return false;
	} else {
		return true;
	}
}


function newsletters(action){
	
	  $(".nlResult").html("");
	  if($("#nltext").val()=="" || !validateEmail($("#nltext").val()) )
	  {
		
		  $(".nlResult").html("<div class='nlError'>من فضلك ادخل بريد الكترونى صحيح</div>");	  
		  return false;	
	  }
	   $(".nlLoading").show();
	   $.post(baseUrl+"newsletters/"+action,{"email":$("#nltext").val()},function(data){
		 
		  $(".nlResult").html(data);
		  $(".nlLoading").hide();
		});
	   
	   	
	
		
	 
	 }

function bookmark()
{
if (document.all)
window.external.AddFavorite(bookmarkurl,bookmarktitle)
else if (window.sidebar) // firefox
window.sidebar.addPanel(bookmarktitle, bookmarkurl, "");
}
	 
	 




function checkVote(){
	if(!$("#voteForm input[type=radio]:checked").val())
	 {
	 
	 alert("من فضلك قم بالتصويت");
	 return false;
	 }
}


