/*
  $Id: common-functions.js,v 1.22 2010/06/03 13:16:18 Franta Exp $

  *************************************************
  *** V3 common javascript functions            ***
  *************************************************
*/

// -----------------------------------------------------------------------------
// debug messages
// -----------------------------------------------------------------------------

var globalDoAlerts = false;

function dbg( what, level, doAlert ) {
	if( window.console ) {
		if( ! level ) level = 3;
		switch( level ) {
			case 1 : console.error( what ); break;
			case 2 : console.warn( what ); break;
			case 3 : console.info( what ); break;
			default : console.log( what );
		}
	} else if( window.opera && window.opera.postError ) {
		opera.postError( what );
	} else if( doAlert || globalDoAlerts ) {
		alert( what );
	}
}

// firebug window.console dummy implementation  (prevents errors in client pages with no firebug installed )
if (!window.console){
    window.console = {};
}
if (!console.firebug){
    var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
    "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
    try{
        for (var i = 0; i < names.length; ++i)
            if (!(names[i] in window.console)) window.console[names[i]] = function() {}
    } catch(e){
     //do nothing
    }
}

// -----------------------------------------------------------------------------
// flash setup
// -----------------------------------------------------------------------------

jQuery( document ).ready( function() {
	
	// get old flash setting
	var oldFlashStatus = jQuery.cookie( "flash" );
	if( ! oldFlashStatus ) oldFlashStatus = "on";
	
	dbg( "Flash plugin available : " + FlashDetect.installed );
	dbg( "Flash plugin version > 8 : " + FlashDetect.versionAtLeast( 8 ) );
	dbg( "Flash cookie at page load : " + oldFlashStatus );

	if(
		FlashDetect.installed && FlashDetect.versionAtLeast( 8 ) &&
		oldFlashStatus == "off"
	) {
		dbg( "Flash already installed. Setting flash cookie back to on ..." );
		// we actually delete it ...
		jQuery.cookie( "flash", null, { path : "/" } );
		dbg( "Reloading page ..." );
		window.location.reload( true );
	}

	/* redirect to the no flash page if flash cookie is "on" & no flash player
	or player version is older than v.8; user can download latest flash player
	or select html version; flash cookie is then set back to "off" & page is
	redirected back to original url */
	if(
		( ! FlashDetect.installed || ! FlashDetect.versionAtLeast( 8 ) ) &&
		oldFlashStatus == "on"
	) {
		dbg( "No usable flash plugin found." );
		if( jQuery( ".noFlash" ).length > 0 ) {
			dbg( "No flash page. Setting up HTML version link ..." );
			// image version link
			jQuery( ".imageLink" ).click( function() {
				dbg( "Setting flash cookie to off ..." );
				jQuery.cookie( "flash", "off", { expires : 42, path : "/" } );
				jQuery( this ).unbind();
				var url = unescape( getUrlParam( "url" ) );
				dbg( "Redirecting to original page ( " + url + " ) ..." );
				// set location from url parameter
				window.location.replace( url );
			} );
		} else {
			// go to no flash page
			var url = "/utils/no-flash.jhtml?url=" + escape( window.location.href );
			dbg( "Redirecting to no flash page ( " + url + " ) ..." );
			window.location.replace( url );
		}
	}
	
} );

/*
	This function returns a value of the url parameter 'name'
*/
function getUrlParam( 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];
}

/*
	This function returns object with all non-empty url parameters as properties
	for http://www.example.com/?first=1&second=2&third= it returns { first : "1", second: "2"} ==> getUrlParams()['first'] returns "1" 
*/
function getUrlParams()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        if (hash[1]) {
          vars.push(hash[0]);
          vars[hash[0]] = hash[1];
        }
    }
    return vars;
}

// The funcion opens a new window. URL, name and windowFeatures are arguments.
// The function is used in <A HREF="javascript:openNewWindow(...)"> tag, where it is not possible to use the standard window.open() function
function openNewWindow(aUrl, aWindowName, aWindowFeatures) {
  window.open(aUrl, aWindowName,aWindowFeatures);
}

// This function resizes iframe under the flipping catalog when the product arrangement is changed. This
// function is called from the included iframe.
function SetFlippCatIframeHeight(pIframeId, pHeight){
	jQuery(pIframeId).css("height",pHeight);
}

// This function handles 'enter' key on form field and it clicks on relevant submit button.
// It fixes the behavior of the IE. It expects the input submit tag to be a direct child of the form tag.

jQuery(function(){
  jQuery('input').keydown(function(e){
  	if (e.keyCode == 13) {
    	var cosik = jQuery(this).parents('form').find('input[type=submit]');
      cosik.click();
      return false;
    }
  });
});

// This feature adds a class "last-li" to all last elements in lists
jQuery(function(){
  jQuery("ul li:last-child").addClass("last-li");
});

