// This code id part of Cascada CSS Framework (C) 2009-2010

var browserName=navigator.appName;
var browserVer=navigator.appVersion;
var thatImage;
var operaImage;
var increment = 0;
var pathToImageDir='img/'; // set path to the image dir where are backgrounds for 'join-background-helpers'
var ep3_current_step;

/* join background helpers vars */
var jbh_rewriting = false; // turns on/off rewriting the CSS selectors in element with 'jbh_rewrite' ID parametr
var jbh_rewrite_dir = '../img/'; //relative (to css sheet) path to place where images are
var jbh = false; // turns on/off join-background-helpers

function closePopup() {
	jQuery('#popupwindow').hide()
}

jQuery(document).ready( function() {
	
	jQuery('#panelmenu > ul > li > a').attr("href",'javascript:void(0)')
	jQuery('#panelmenu > ul > li > a').css("cursor",'default')  
	
	if (jQuery('.ccollistawycieczeksafari').size()>0) {
		jQuery('.B_crumbBox').addClass('breadcrumbleft');
	}
    
    if (jQuery('.quicksearchtable').size()>0) {
        jQuery('#submit').attr('src','/img/type3.gif');
        jQuery("#maint_search").change(function() {  
        	allinclusiveDD(jQuery('#maint_search').val())
        });
        jQuery('#maxpreis').parent().parent().hide();

        var hotelStars = "<td id=\"category\" colspan=\"3\">Liczba gwiazdek:<br/>"
            hotelStars += "<select style=\"width: 167px;\" name=\"s\" class=\"startseite_wizard_input_halb\" id=\"category_search\">"
                hotelStars += "<option selected=\"selected\" value=\"\">obojętnie</option>"
                    hotelStars += "<option value=\"2\">od 2 gwiazdek</option>"
                        hotelStars += "<option value=\"3\">od 3 gwiazdek</option>"
                            hotelStars += "<option value=\"4\">od 4 gwiazdek</option>"
                                hotelStars += "<option value=\"5\">od 5 gwiazdek</option>"
                                    hotelStars += "</select>"
                                        hotelStars += "</td>";
        
        hotelStars="";
        

    jQuery('.quicksearchtable .color6').each( function(){

        if (trim(jQuery(this).html())=='Cena max.') {

            if (jQuery('#moreCriteriaLink').css('display') !== 'none') {
                jQuery(this).parent().parent().html(hotelStars);
            } else {
                jQuery(this).parent().parent().hide();
            }
            
        }
            
    })      
    
    jQuery('.quicksearchtable > tbody > tr:nth-child(2) > td').append('<input onclick="allinclusive()" type="checkbox" id="allinclus" name="" class="fl" /><label for="allinclus"  onclick="allinclusive()" class="allinclus fl">All Inclusive </label><div class="cb"></div>')
        
    }
    
    jQuery('#hity li:nth-child(1)').addClass('hit_numer_1');
    jQuery('#hity li:nth-child(2)').addClass('hit_numer_2');
    jQuery('#hity li:nth-child(3)').addClass('hit_numer_3');
    
    jQuery('.pricehandler').hover(
    function(){
        jQuery(this).find('span.pricecloud').show();
    },
    function(){
        jQuery(this).find('span.pricecloud').hide();
    }
    );
    
    if (ep3_current_step=='6') {
        jQuery('#reservation td.color0bg:first-child strong').each( function() {
            if (jQuery(this).html()=='5. Warunki umowy') {
                jQuery(this).remove();
            }
        })
        
        
        
        jQuery('input[name$="accept"]').parent().parent().remove();
        jQuery('input[name$="accept_giodo"]').parent().parent().remove();
        jQuery('input[name$="accept_email"]').parent().parent().remove();
        
        
        
    }
    
    jQuery('.infoslinks li:last-child').addClass('last');
    jQuery('.infoslinks2 li:last-child').addClass('last');
    
    
    jQuery('h2.cont_box_header').each( function(){
        if (jQuery.trim(jQuery(this).html())=="")
            jQuery(this).remove();
    });
    
    jQuery('h2.cont_box_header a').each( function(){
        if (jQuery.trim(jQuery(this).html())=="")
            jQuery(this).parent().remove();
    });
    
    jQuery('#infoslinks2').each( function(){
        if (jQuery.trim(jQuery(this).html())=="")
            jQuery(this).remove();
    });
    
    if (jQuery('.hideparent').parent())
        jQuery('.hideparent').parent().remove();
    
    jQuery('.boxeshomedescr h3').hover( 
    function(){
        jQuery(this).find('vishid').css('visibility','visible')
    },
    function(){
        jQuery(this).find('vishid').css('visibility','hidden')
    }
    )
    
    jQuery('#infoslinks li:last-child').addClass("last")
    jQuery('#infoslinks2 li:last-child').addClass("last")
    
    jQuery('#navmenu .ep3_done:first-child').remove();
    
    var thisHtmlincrem=1;
    jQuery('#navmenu tr td').each( function(){
        var thisHtml = jQuery(this).html();
        if (jQuery(this).attr('valign')==undefined) {
            jQuery(this).html(thisHtmlincrem+'. '+thisHtml.substring(10,30))
            thisHtmlincrem++
        }
            
            
    })
    
    
    jQuery('table.color1bob td.color2bot span.color1').parent().attr('align','left')
    
    
    if (jQuery('.twocolcontent .imageheader span span')) {
    
    var h2inner = jQuery('.twocolcontent .imageheader span span').html();
    if (h2inner) {
        var newW = (parseInt(h2inner.length))*10+50;
        jQuery('.twocolcontent .imageheader').css('width',newW+'px')
    }
    
    }
    
    jQuery('.triplist li:odd').addClass('odd')
    
    //clear all values in text-type inputs after click on them:
//  jQuery('input').click( function() {
//      if (jQuery(this).attr('type')=="text") {
//          jQuery(this).attr('value','');          
//      }
//  })      
    
    jQuery('#header1 .makemenu li a').each( function(){
        
        jQuery(this).html('<span>'+jQuery(this).html()+'</span>');
        
    })
    
    jQuery('#foot .makemenu li h3:last').addClass('lastfoot')
    
    //adding the corners with these parameters to everyone div with 'corner' class
    //jQuery('.corner').corner("round 8px").parent().css('padding', '2px').corner("round 10px");
    //jQuery('.corner').parent().css('background-color', '#E1E3E2');
    //to use that - paste file inclusion of jquery.corner.js in HEAD section:
    //<script type="text/javascript" src="js/jquery.corner.js"></script>
    
    if (browserName == "Microsoft Internet Explorer" && browserVer.indexOf(6.0) > -1) {
    // this one should runs on ie6
    }
    
    // specify style sheets for browsers: //
    // when directory structure are different, below paths to sheets should be changed! // 
    
    if (browserName == "Microsoft Internet Explorer" && browserVer.indexOf(7.0) > -1) {
        //alert('welcome ie7 user!');
        jQuery('head').append('<link rel="stylesheet" href="./css/ie7.css" media="screen" type="text/css" />');
    } 
    
    if (browserName == "Opera") {
        //alert('welcome opera user!');
        jQuery('head').append('<link rel="stylesheet" href="./css/opera.css" media="screen" type="text/css" />');
    }
    
    if ((navigator.userAgent.indexOf("Firefox")!=-1)) {
        //alert('welcome firefox user!');
        jQuery('head').append('<link rel="stylesheet" href="./css/firefox.css" media="screen" type="text/css" />');     
    }
    
    if ((navigator.userAgent.indexOf("Safari")!=-1)) {
        //alert('welcome safari user!');
        jQuery('head').append('<link rel="stylesheet" href="./css/safari.css" media="screen" type="text/css" />');      
    }
    
    if (navigator.userAgent.toLowerCase().indexOf('chrome')>0) {
        //alert('welcome google chrome user!');
        jQuery('head').append('<link rel="stylesheet" href="./css/gchrome.css" media="screen" type="text/css" />');     
    } 
    
    
    
    // etc
    
    //join-background-helpers - the usage of this feature is in files readme.txt and join-backgroun-helper-example.html
    
    if (jbh) {
        jQuery('.jjpg').each(function(){
            joinBgHelper(jQuery(this), 'jpg');
        });
        jQuery('.jjpeg').each(function(){
            joinBgHelper(jQuery(this), 'jpeg');
        });
        jQuery('.jgif').each(function(){
            joinBgHelper(jQuery(this), 'gif');
        });
        jQuery('.jpng').each(function(){
            joinBgHelper(jQuery(this), 'png');
        });
        jQuery('.jbmp').each(function(){
            joinBgHelper(jQuery(this), 'bmp');
        });
    }

//multiple join-background-helpers - the usage of this feature is in files readme.txt and join-backgroun-helper-example.html

    if (jbh) {
    jQuery('.jmjpg').each( function() {
        joinMultipleBgHelper(jQuery(this),'jpg');
        joinBgHelperMultiple(jQuery(this), 'jpg');
    });
    jQuery('.jmgif').each( function() {
        joinMultipleBgHelper(jQuery(this),'gif');
        joinBgHelperMultiple(jQuery(this), 'gif');
    });     
    jQuery('.jmpng').each( function() {
        joinMultipleBgHelper(jQuery(this),'png');
        joinBgHelperMultiple(jQuery(this), 'png');
    }); 
    jQuery('.jmbmp').each( function() {
        joinMultipleBgHelper(jQuery(this),'bmp');
        joinBgHelperMultiple(jQuery(this), 'bmp');
    });     
    }
});

/*
example of jquery flash-pluggin usage:
jQuery(document).ready(function(){
jQuery('#flashbanner').flash(
    { src: 'img/flash/file.swf',
      width: 694,
      height: 401 }
);
});
*/

function joinBgHelper(jbh_object,extention) {
        jbh_object.css("background-image","url("+pathToImageDir+"/"+jbh_object.attr("id")+"."+extention+")");
        if (jbh_object.is('.s')) {
            var path = pathToImageDir+jbh_object.attr("id")+"."+extention;
            path = path.replace(/^\s*|\s*$/g,'');
            setSixeFromImage(path,jbh_object)
        }   
}

function joinBgHelperMultiple(jbh_object,extention) {
jbh_object.css("background-image","url("+pathToImageDir+"/"+jbh_object.attr('class').split(' ').slice(-1)+"."+extention+")");
if (jbh_object.is('.s')) {
    var path = pathToImageDir+jbh_object.attr('class').split(' ').slice(-1)+"."+extention;
    path = path.replace(/^\s*|\s*$/g,'');
    setSixeFromImage(path,jbh_object)
}   
}

function joinMultipleBgHelper(jbh_object,extention) {
var bground = jbh_object.attr('class').split(' ').slice(-1);
jbh_object.css("background-image","url("+pathToImageDir+"/"+bground+"."+extention+")");
        /*if (jbh_object.is('.s')) {
            if (navigator.userAgent.indexOf('Safari') != -1) {
                jbh_object.html(jbh_object.html()+'img class="hide" name="' + jbh_object.attr('class').split(' ').slice(-1) + '" src="' + pathToImageDir + "/" + jbh_object.attr('class').split(' ').slice(-1) + '.'+extention+ '" /');
                jQuery('img').load(callback).each(callback);
            } else {
                var path = pathToImageDir+bground+"."+extention;
                setSixeFromImage(path, jbh_object, false, false)
            }
        }*/
}

function setSixeFromImage(urlPath,jbh_object,w,h) {
        if (navigator.userAgent.indexOf('Opera') != -1) {
            thatImage = new Image();
            thatImage.src = urlPath;
            jbh_object.css("display", "block");
            jbh_object.css("width", thatImage.width + "px");
            jbh_object.css("height", thatImage.height + "px");
            jbh_object.html(jbh_object.html() + '<img class="hide" id="joinimageobject_' + increment + '" src="' + thatImage.src + '">');
            jQuery('#joinimageobject_' + increment).load( function() {
                jQuery(this).parent().css("width",jQuery(this).width()+"px");
                jQuery(this).parent().css("height",jQuery(this).height()+"px");
            })
            increment++;    
        } else {
            thatImage = new Image();
            thatImage.src = urlPath;
            jbh_object.css("display", "block");
            jbh_object.css("width", thatImage.width + "px");
            jbh_object.css("height", thatImage.height + "px");
            jbh_object.html(jbh_object.html() + '<img class="hide" id="joinimageobject_' + increment + '" src="' + thatImage.src + '">');
            jQuery('#joinimageobject_' + increment).load(callback).each(callback);
            increment++;                
        }       
}

function callback(){
var el = jQuery(this);
if(el.data('loaded') || el.width() === 0)
    return;
el.data('loaded', 1);
//this one based on post: http://stackoverflow.com/questions/1126272/are-image-properties-part-of-the-dom-jquery-webkit-inconsistency-with-other-br

el.parent().css("height", el.height() + "px");
el.parent().css("width", el.width() + "px");
el.parent().html(el.parent().html());
}

jQuery(document).ready( function() {

if (jbh_rewriting && jbh) {
    jbh_rewritings('jpng');
    jbh_rewritings('jjpg');
    jbh_rewritings('jgif');
    jbh_rewritings_MULTIPLE('jmpng');
    jbh_rewritings_MULTIPLE('jmjpg');
    jbh_rewritings_MULTIPLE('jmgif');
}

//jbh_rewritings('jjpeg');
//jbh_rewritings('jbmp');   


    
});

function jbh_rewritings(what) {

alert('rewriting css of '+what);
jQuery('.'+what).each(function(){
    var exten = jQuery(this).attr("style");
    exten = exten.replace(pathToImageDir, '../img/');
    document.getElementById("jbh_rewrite").innerHTML += '#' + jQuery(this).attr("id");
    document.getElementById("jbh_rewrite").innerHTML += ' {<br/>';
    document.getElementById("jbh_rewrite").innerHTML += exten;
    document.getElementById("jbh_rewrite").innerHTML += '<br/>}<br/><br/>';
});
jQuery('#jbh_rewrite').css('background-color','yellow');
jQuery('#jbh_rewrite').css('color','red');
}

function jbh_rewritings_MULTIPLE(what) {

    alert('rewriting css of '+what);
    jQuery('.'+what).each(function(){
        var exten = jQuery(this).attr("style");
        exten = exten.replace(pathToImageDir, '../img/');
        document.getElementById("jbh_rewrite").innerHTML += '.' + jQuery(this).attr('class').split(' ').slice(-1);
        document.getElementById("jbh_rewrite").innerHTML += ' {<br/>';
        document.getElementById("jbh_rewrite").innerHTML += exten;
        document.getElementById("jbh_rewrite").innerHTML += '<br/>}<br/><br/>';
    });
    jQuery('#jbh_rewrite').css('background-color','yellow');
    jQuery('#jbh_rewrite').css('color','red');
}   

function setCookie(name,value,days) {
if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}


/* usage
setCookie('SomeCookieName','nnaammee');
if (getCookie('SomeCookieName')!=='nnaammee') {
    do this
}
*/

function findContext(merlinxbox) {
    
    var bodyCC=jQuery('#page .ccol').html();
    
    $.post("/context.php", { name: bodyCC },
               function(data) {
                    jQuery('#context').html(data);
                    jQuery('#context').addClass('nobg');
               });  
    
}

//function findContext(merlinxbox) {
//  $.ajax({
//        url: '/context.php?merlinxbox='+merlinxbox,
//        success: function(data) {
//              jQuery('#context').html(data);
//              jQuery('#context').addClass('nobg')
//          }
//    });       
//}

function explode (delimiter, string, limit) {
    // http://kevin.vanzonneveld.net
    // +     original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     improved by: kenneth
    // +     improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     improved by: d3x
    // +     bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: explode(' ', 'Kevin van Zonneveld');
    // *     returns 1: {0: 'Kevin', 1: 'van', 2: 'Zonneveld'}
    // *     example 2: explode('=', 'a=bc=d', 2);
    // *     returns 2: ['a', 'bc=d']
    var emptyArray = {
        0: ''
    };

    // third argument is not required
    if (arguments.length < 2 || typeof arguments[0] == 'undefined' || typeof arguments[1] == 'undefined') {
        return null;
    }

    if (delimiter === '' || delimiter === false || delimiter === null) {
        return false;
    }

    if (typeof delimiter == 'function' || typeof delimiter == 'object' || typeof string == 'function' || typeof string == 'object') {
        return emptyArray;
    }

    if (delimiter === true) {
        delimiter = '1';
    }

    if (!limit) {
        return string.toString().split(delimiter.toString());
    } else {
        // support for limit argument
        var splitted = string.toString().split(delimiter.toString());
        var partA = splitted.splice(0, limit - 1);
        var partB = splitted.join(delimiter.toString());
        partA.push(partB);
        return partA;
    }
}

function addslashes (str) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Ates Goral (http://magnetiq.com)
    // +   improved by: marrtins
    // +   improved by: Nate
    // +   improved by: Onno Marsman
    // +   input by: Denny Wardhana
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   improved by: Oskar Larsson Högfeldt (http://oskar-lh.name/)
    // *     example 1: addslashes("kevin's birthday");
    // *     returns 1: 'kevin\'s birthday'
    return (str + '').replace(/[\\"']/g, '\\$&').replace(/\u0000/g, '\\0');
}

function showHit(LYNK) {
    LYNK=LYNK.substring(1,LYNK.length)
    jQuery('#simpleoverlay').show();
    jQuery('#window').show();
    jQuery('#window .inner').load('/'+LYNK);
//    jQuery('body').css('overflow','hidden');
}

function str_replace (search, replace, subject, count) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Gabriel Paderni
    // +   improved by: Philip Peterson
    // +   improved by: Simon Willison (http://simonwillison.net)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   bugfixed by: Anton Ongson
    // +      input by: Onno Marsman
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    tweaked by: Onno Marsman
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   input by: Oleg Eremeev
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Oleg Eremeev
    // %          note 1: The count parameter must be passed as a string in order
    // %          note 1:  to find a global variable in which the result will be given
    // *     example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
    // *     returns 1: 'Kevin.van.Zonneveld'
    // *     example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
    // *     returns 2: 'hemmo, mars'
    var i = 0,
        j = 0,
        temp = '',
        repl = '',
        sl = 0,
        fl = 0,
        f = [].concat(search),
        r = [].concat(replace),
        s = subject,
        ra = Object.prototype.toString.call(r) === '[object Array]',
        sa = Object.prototype.toString.call(s) === '[object Array]';
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }

    for (i = 0, sl = s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j = 0, fl = f.length; j < fl; j++) {
            temp = s[i] + '';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length - s[i].length) / f[j].length;
            }
        }
    }
    return sa ? s : s[0];
}




function showHitForm(LYNK,Ptitle,TripDate,TripBoat,TripRoute) {
    LYNK=LYNK.substring(1,LYNK.length)
    
    TripDate=str_replace(' ', '-', jQuery.trim(TripDate));
    TripBoat=str_replace(' ', '-', jQuery.trim(TripBoat));
    TripRoute=str_replace(' ', '-', jQuery.trim(TripRoute));
    Ptitle=str_replace(' ', '-', jQuery.trim(Ptitle));
    
    jQuery('#simpleoverlay').show();
    jQuery('#window').show();
    
    jQuery('#window .inner').load('/'+LYNK+'&tripname='+Ptitle+'&triproute='+TripRoute+'&tripdate='+TripDate+'&tripboat='+TripBoat);
//    jQuery('body').css('overflow','hidden');
}

function closeTopLayer() {
    jQuery('#simpleoverlay').hide();
    jQuery('#window').hide();
    jQuery('body').css('overflow','scroll');
}

function showInCenter(LYNK) {
    alert(LYNK)
    jQuery('#page').load('/'+LYNK);
}

function trim (str, charlist) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: mdsjack (http://www.mdsjack.bo.it)
    // +   improved by: Alexander Ermolaev (http://snippets.dzone.com/user/AlexanderErmolaev)
    // +      input by: Erkekjetter
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: DxGx
    // +   improved by: Steven Levithan (http://blog.stevenlevithan.com)
    // +    tweaked by: Jack
    // +   bugfixed by: Onno Marsman
    // *     example 1: trim('    Kevin van Zonneveld    ');
    // *     returns 1: 'Kevin van Zonneveld'
    // *     example 2: trim('Hello World', 'Hdle');
    // *     returns 2: 'o Wor'
    // *     example 3: trim(16, 1);
    // *     returns 3: 6
    var whitespace, l = 0,
        i = 0;
    str += '';

    if (!charlist) {
        // default list
        whitespace = " \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000";
    } else {
        // preg_quote custom list
        charlist += '';
        whitespace = charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '$1');
    }

    l = str.length;
    for (i = 0; i < l; i++) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {
            str = str.substring(i);
            break;
        }
    }

    l = str.length;
    for (i = l - 1; i >= 0; i--) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {
            str = str.substring(0, i + 1);
            break;
        }
    }

    return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
}

function allinclusive() {
    if (jQuery('#allinclus').attr('checked')==true) {
        jQuery('#maint_search').val('1');
    } else {
        jQuery('#maint_search').val('');
    }   
}

function allinclusiveDD(thisVAL) {
    if (thisVAL=='1') {
    	jQuery('#allinclus').attr('checked','checked')
    } else {
    	jQuery('#allinclus').removeAttr('checked')
    }
}
