// 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;
	}
}


var videoURL = "";

//set video for flash
function setVideoURL(videoURL) {
	var e = swfobject.getObjectById("fobject");
    e.getVideoURL(videoURL);
}

//show videoplayer
function showPlayer(width, height, video, templateURL) {
   	
   	var params = { allowScriptAccess: "always" };
   	var flashvars = { videoURL: video };
	var attributes = {
	  id: "fobject",
	  name: "fobject"
	};

	//if we have a swfobject already we just send the movie to the swf
	if($("object[name=fobject]").is(':visible')) {
		
		setVideoURL(video);
		
	//otherwise we create it	
	} else {
		
		swfobject.embedSWF(templateURL + "/swf/videoplayer.swf", "fobjectloader", width, height, "8.0.0", templateURL + "/swf/expressInstall.swf", flashvars, params, attributes);
		
	}

}

//CLEAR FIELDS ON FOCUS
function clearOnFocus(obj,type ) {
	var prevtext = $(obj).val();
	var orgtext = $(obj).attr( 'title' );
	
	if( type == 'focus' )
	{
	    if ( prevtext == orgtext )
	        $(obj).val( '' );
    }
    else
    {
	    if ( prevtext == '' )
	        $(obj).val( orgtext );
    }
}

//show respons after ajax form
function showResponseContact(responseText, statusText) {
	
	$("div.inputs","form[name=contactform]").fadeOut(500, function() {
		$("p","div#contactrespons").html(responseText);
		$("div#contactrespons").fadeIn(400);
		Cufon.refresh('.gothamrounded');
	});
}

//show respons after ajax form
function showResponseBook(responseText, statusText) {
	
	$("div.inputs","form[name=booktime]").fadeOut(500, function() {
		$("p","div#bookrespons").html(responseText);
		$("div#bookrespons").fadeIn(400);
		
		Cufon.refresh('.gothamrounded');
		
	});
}

var mouse_over_langmenu = false;

$(document).ready(function(){

    //clear inputs text on blur
	$("input.clearfocus").focus(function() { clearOnFocus(this,'focus'); });
	$("input.clearfocus").blur(function() { clearOnFocus(this,'blure'); });
	
	//generic toggle
	$("a.toggleobj").click(function() {
				
		var obj = this.hash;
		$(obj).toggle();
		$(this).toggleClass('active');
		
		return false;
	}); 
	
	//submit form
	$(".submitform").click(function() {
		var obj = this.hash;
		$(obj).submit();		
		return false;
	});
	
	jQuery.validator.addMethod(
	    "contact_info",
	    function( value, element )
	    {
	        if( value == '' )
	            if( $("#contactform #email").val() == '' )
	                return false;
	                
            return true;
        },
        "Wrong.. try again pls.." );

	//contact form validate
	$("form[name=contactform]").validate({
	    
	    // Validation rules
	    rules :
	    {
	        phone:
            {
                contact_info: true
	        }
	    },

		// If errors
		invalidHandler: function( form, validator )
		{
		    // Marking all as valid..
		    for( var i = 0,elements = validator.elements() ; i < elements.length; i++ )
		        $( '#' + elements[ i ].id ).css({'border' : '0'});
		        
		    // Marking the fields thats not valid
		    for( var i = 0,elements = validator.invalidElements() ; i < elements.length; i++ )
		        $( '#' + elements[ i ].id ).css({ 'border' : '1px solid #f00' });
		        
	        // pop-up with error info..
            $.fancybox(
                $( '#form-error' ).html(),
                {
                    'height'        : 'auto',
                    'transitionIn'  : 'fade',
                    'transitionOut' : 'fade',
                    'overlayShow'	: true,
                    'overlayOpacity': '0.9',
                    'overlayColor'  : '#000',
                    'centerOnScroll': true
                }
            );
		},

		// if submit
		submitHandler: function(form)
		{
			$(form).ajaxSubmit({
				success:   showResponseContact,
				clearForm: 'true'
			});
		}
	});
	
	
	//tab menu
	$("a","ul.tabmenu").click(function() {
	
		$("li.active","ul.tabmenu").removeClass('active');
		$(this).parent().addClass('active');
		
		//remove content
		$("div.tabitem","div.taboptions ul li.active").hide();
		$("li.active","div.taboptions ul").removeClass('active');
		
		var tabo	= 	$(this).attr('href');
		
		$("ul.active","div.taboptions").removeClass('active');
		$(tabo).addClass('active');
		
		return false;
		
	});
	
	//calculate height
	$("div.taboptions ul li div.tabitem").each (function() { $(this).css("height", $(this).height()); });

	//then hide the items
	$("div.taboptions ul li div.tabitem, div.taboptions ul").hide();

	
	//tab options
	$("a.linkitem","div.taboptions ul").click(function() {
	
		var nli	= $(this).parent();
	
		$("div.tabitem","div.taboptions ul li.active").slideUp(300, function() {
			$("li.active","div.taboptions ul").not(nli).removeClass('active');
		});
		
			var wait = setInterval(function() {
				if(!$("div.tabitem","div.taboptions ul li.active").is(":animated")) {
					clearInterval(wait);
					
					if(!nli.hasClass('active')) {	
						nli.addClass('active');
						$("div.tabitem",nli).slideDown(300);
					} else {
						nli.removeClass('active');	
					}
					
				}
			}, 200);

			return false;
		
	});
	
	//toggle video list
	$("a","div#filmselector_header").click(function() {
	
		var obj = this.hash;
		$(obj).slideToggle();
	
		return false;
		
	});
	
	//replace submit to work with cufon
	/*$('.submit').each(function(){
        $(this).after(unescape('%3Cspan class="submit"%3Eaa%3C/span%3E'));
        $(this).hide();
        $(this).next('span.submit').text($(this).val()).click(function(){
            $(this).prev('input.submit').click();
        });
    });
    
    Cufon.set('fontFamily', 'Gotham Rounded Medium').replace('.submit');
	*/	
	
	//doljer rubriker
	$('h1').each(function(index) {
	
		if( $(this).hasClass("heading") ) {
			$(this).css("display","none");	
		}
	
	});
	    	
});

