// PARAMETER FUNCTIONS
function getParams(from) {
	var params = {};
	if($(from).attr("params")){
		$.each($(from).attr("params").split(","), function(){		
			var thiskey = this.split(":")[0];
			//var thisval = this.split(":")[1]; 
			// enhanced for url
			var thisval = this.substr(this.split(":")[0].length+1,this.length);
			tmpobj = {};
			eval("tmpobj." + thiskey + "=thisval");
			jQuery.extend(params,tmpobj);	
		});
		return params;
	}
}

//PARAMS TO PATH FUNCTION
function paramsToPath(obj) {
	if(obj) {
		var pairs = [];
		var path = "";
		var del1 = "=";
		var del2 = "&";
		$.each(obj, function(i,val) { 
			if(val!=""){ pairs.push(i+del1+encodeURIComponent(val)); }//values are encoded to pass urls etc. 
		});
		path = pairs.join(del2);
		return path;
	}
}

//ajax function to retrive the work pages
function getWorkPage(obj) {
	
	var params = getParams(obj);
		
	$.ajax({
		url: '?page_id=52&workid='+params.workID,
		type: 'GET',
		dataType: 'html',
		timeout: 30000,
			
			error: function(XMLHttpRequest, textStatus, errorThrown){ alert(XMLHttpRequest+", "+textStatus+", "+errorThrown); },
			
			beforeSend: function(){
				$("#workpost"+params.workID+" div.preajax").animate({opacity:0}, 500);
				$("#workpost"+params.workID+" img.ajaxloader").show();
			},
			success: function(data){
								
				//smoothscroll
				$.scrollTo("#workpost"+params.workID, {duration:800});
				
				//fade out loader img
				$("#workpost"+params.workID+" img.ajaxloader").fadeOut(300);
				
				$("#workpost"+params.workID+" div.postajax").html(data);
								
				//hide prev
				$("#workpost"+params.workID+" div.preajax").hide();
				
				//animate and show final content
				$("#workpost"+params.workID+" div.postajax").show().animate({opacity:1}, 500, function() {});
				
				//reset add this
				$("#workpost"+params.workID+" .addthis_button").click(function() {
					var tt = $(this).attr('addthis:title');
					var tu = $(this).attr('addthis:url');
					
					addthis_open(this, '', tu, tt);
					return false;
				});
				
				jQuery(".gdsr_rating_as > a").click(function() { gdsr_rating_standard(this); });
				
				//active fancybox
				$('a.lightbox').fancybox();
				
				$("a.morefilms").click(function() {
					$(this.hash).show();
				});
				
				//smooth scroll
				$.localScroll();
				
				if($("#workpost"+params.workID+" div.ngg-galleryoverview").length) {
					$("#workpost"+params.workID+" a.behindpics").show();
					
					$("#workpost"+params.workID+" div.ngg-galleryoverview").clone(true).appendTo("#workpost"+params.workID+" div.behindpics_wrapper");
					
					$("a.behindpics").click(function() {
						$("#workpost"+params.workID+" div.behindpics_wrapper div.ngg-galleryoverview").toggle();
					});
				}
				
				//close project
				$("#workpost"+params.workID+" a.closebtn").show().click(function() {
				
				
					//animate and show final content
					$("#workpost"+params.workID+" div.postajax").animate({opacity:0}, 500, function() {
					
						//hide prev
						$("#workpost"+params.workID+" div.preajax").show().animate({opacity:1}, 500, function() {});	
						
						$(this).hide();
						
					});
					
				});
				
			}			
		});
}

//after submit of faq form
function showResponseFaq(responseText, statusText) {
	$("#faq_form").fadeOut(500, function() {
		$(this).remove();
		$("div.ajaxrespons").html(responseText).fadeIn();	
	});
}

//after submit of faq form
function showResponseTalent(responseText, statusText) {
	$("div.ajaxrespons").html(responseText).fadeIn();	
}

//CLEAR FIELDS ON FOCUS
function clearOnFocus(obj) {
	var prevtext = $(obj).val();
	var orgtext = $(obj).attr("title");

	if ((prevtext != orgtext) && (prevtext != "")) {
		//do nothing...
	} else if(prevtext == "") {
		$(obj).val(orgtext);
	} else {
		$(obj).val("");
	}

} 

$(document).ready(function(){    
   
	$("a.loadcontent").click(function() {	
		getWorkPage(this);
		return false;
	});
	
	//active fancybox
	$('a.lightbox').fancybox();
	
	//fix for gallery	
	if($("div.workposts-wrapper div.ngg-galleryoverview").length) {
		$("div.workposts-wrapper a.behindpics").show();
		
		$("div.workposts-wrapper div.ngg-galleryoverview").clone(true).appendTo("div.workposts-wrapper div.behindpics_wrapper");
		
		$("a.behindpics").click(function() {
			$("div.workposts-wrapper div.behindpics_wrapper div.ngg-galleryoverview").toggle();
		});
	}
	
	//faq form ajax
	$('#faq_form').ajaxForm({
		success: showResponseFaq,
		clearForm: 'true'
	});
	
	//faq form ajax
	$('#talent_form').ajaxForm({
		success: showResponseTalent
	});
	
	//set cookie number in menu
	$('sup.getcookie').each(function(index) {
		var params = getParams($(this));
		$(this).clone().appendTo("li#menu-item-"+params.menuitem);
		
	});
	
	//clear inputs text on blur
	$("input.clearfocus").focus(function() { clearOnFocus(this); });
	$("input.clearfocus").blur(function() { clearOnFocus(this); }); 
	
	//smooth scroll
	$.localScroll();
    	
});
