﻿/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;
var ref= 0;


//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		
		$("#popup").fadeIn("slow");
		$("#popupcontent").fadeIn("slow");
		popupStatus = 1;
		
	}
}
function removeFrame(){
	//check for iframe
		iframes=document.getElementsByTagName('iframe');
		for(i=0;i<iframes.length;i++){
		iframe=iframes.item(i);
		iname=iframe.name;
			if(iframe&&iname=='toprint')
				document.body.removeChild(iframe);
		}
}
//disabling popup with jQuery magic!
function disablePopup(){
	
	//disables popup only if it is enabled
	if(popupStatus==1){
		if(!window.XMLHttpRequest)
			$('#ie6-hide,select').css('visibility','visible');
		$("#popup").fadeOut("slow");
		$("#popupcontent").fadeOut("slow");
		popupStatus = 0;
		removeFrame();
		
	}
	
	if(ref==1){
		 ref=0;
	$('#popupcontent').html('').parent().css('display','none');	 
	 //window.location.href = window.location.href;
	
	}
		
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var docHeight=document.body.offsetHeight;
	var popupHeight = $("#popupcontent").height();
	var popupWidth = $("#popupcontent").width();
	
	//centering
	
	if(parseInt(popupHeight)<parseInt(windowHeight)){
		
		$("#popupcontent #cv-details").css({
		"height": windowHeight
		
	});
	}
	
	
	scrolly=document.documentElement.scrollTop;
	popupheight=docHeight+scrolly;
	
	$("#popup").css({
		"height": $('#popup #popupcontent').height()+50,
		"min-height": popupheight
		
	});
	
	//$("#popupcontent").css();
	
	//only need force for IE6
	 if (!window.XMLHttpRequest) {
			
		$("#popup").css({
		"height": popupheight
		});
		
		
		}
	
	
}


//get the parameter name
function getParameterByName( 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 decodeURIComponent(results[1].replace(/\+/g, " "));
}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	
	//LOADING POPUP
	//Click the button event!
	$("a.popup").click(function(){
			
		
			
		r=Math.random()*1000;
		ref=($(this).attr('rel')==''?0:1);

        
			var pagename=window.location.href;
			
		if($(this).hasClass('view_c'))
		{
			if(!$(this).hasClass('limit')){
			if(!window.confirm(view_msg))
			return false;
			else{
			abc=123;
            
			/**aded by maher to modify the number of cv viewd****/
		     var nbcvs=$('#nbcvs').html();
		       ++nbcvs;
			$('#nbcvs').html(nbcvs);
	
	        
			if(pagename.indexOf('cv-search.php') >= 0)
			{
				var lang=getParameterByName('lang');
				$(this).removeClass('view_c').end();
				if($(this).parent().hasClass('cvico2'))
				{
					var pp=$(this).parent().parent();
					pp.find('a.view_c').removeClass('view_c');
				}
				else
				{
					var pp=$(this).parent().parent().parent();
					pp.find('a.view_c').removeClass('view_c');
				}
			    
				if(lang=='en')
				   {
				    pp.find('div.inboxactions').prepend('<div class="viewed-cv">CV Viewed Before</div>');
				   }
				else
				  {
					 pp.find('div.inboxactions').prepend('<div class="viewed-cv">لقد تصفحت هذه السيرة الذاتيه من قبل</div>');
				  }
				 
			      
			}
	      
			  
			 
			
		  /***** end modified cv viewed****/
		   
			//$(this).removeClass('view_c').parent().siblings('div:eq(0)').show().end().siblings('div:eq(1)').remove();
				
			}
			}
		}
		
		 if(pagename.indexOf('inbox.php') >= 0)
				{
					
						$(this).removeClass('view_c').end();
						if($(this).parent().hasClass('cvico2'))
						{
							$(this).parent().parent().find('div.cv-search-box').removeClass('new').addClass('old');
						}
						else
						{
							var pp=$(this).parent().parent().parent().find('div.cv-search-box').removeClass('new').addClass('old');
						}
						
				}
		
		href=$(this).attr('href');
		if(href!='javascript:;'){
		//IE6 combobox bug
		if(!window.XMLHttpRequest)
			$('#ie6-hide,select').css('visibility','hidden');
	
		scrolly=document.documentElement.scrollTop;
		contentMinHeight=document.documentElement.clientHeight;
		
		$('#popupcontent').css({
		
		"margin-top":scrolly,
		"min-height":contentMinHeight
		}).addClass('loading').html('&nbsp;').load(href+'&rr='+r,function(){
			
			$(this).removeClass('loading');
			//centering with css
			centerPopup();
			
			$("#popupContactClose").click(function(){
			disablePopup();
		
	});
		 });
		//load popup
		loadPopup();
		}
		
		return false;
		
	});
				
	//CLOSING POPUP
	//Click the x event!
	$("#popupContactClose").click(function(){
		disablePopup();
		
	});
	
	//Click out event!
	$("#popup").click(function(){
						  
		//disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

});
