$(document).ready(function() {
    /// PREVIEW FOR NAVIGATION
    showNavigationPreview();
	/// FANCYBOX FOR COLLECTION
    collectionFancyBox();
	/// FANCYBOX GALLERY
    fancyBoxFunctionalities();
    /// CACHE KILLER
    addParameterToAvoidCache();
    /// SHOW HIDDEN DETAILS
    revealHiddenDetail();

});

/* check for mobile */
function checkForMobileDevice(){
    var isMobileDevice = false;
    var usAg = window.navigator.userAgent;
    if (screen.width < 500 || usAg.match(/Android/i) || usAg.match(/webOS/i) || usAg.match(/iPad/i) || usAg.match(/iPhone/i) || usAg.match(/iPod/i) || usAg.match(/BlackBerry/i)) {
        isMobileDevice = true;
    }
    return isMobileDevice;
}
function appendMobileDeviceCSS(docRootPath){
    if(checkForMobileDevice()){
        $("head").append('<link rel="stylesheet" type="text/css" media="screen" href="'+docRootPath+'/css/mobile.css" />');
    }
}



function revealHiddenDetail(){
    $(".hiddenDetailContainer h4").click(function(){
        $(".hiddenDetail", $(this).parent()).slideToggle();
    });
}

/* helper methods */
function collectionFancyBox(){
    $(".collection a").fancybox({
//        'centerOnScroll'    : true,
        'padding'           : 0,
        'margin'            : 60,
        'overlayColor'		: '#535353',
        'overlayOpacity'	: 0.9,
        'titlePosition' 	: 'outside',
        'cyclic':'true',
        'titleFormat': formatCollectionTitle,
        'onComplete'        : function(){
            $.fancybox.resize();

        }


    });
}
function formatCollectionTitle(title, currentArray, currentIndex, currentOpts) {
//    document.title = "";
    $(".collection_thumbnav img").each(function(){
        var t = $(this);
        var n = t.attr("id").substring(2);
        if(n==currentIndex){$(this).addClass("active");}else{$(this).removeClass("active");}
//        document.title += n +"/"+currentIndex+" ... ";

    });
    var h = "";
    $(".collection_thumbnav").each(function(){
        h += $(this).html();
    });
    return (title && title.length ?  '<div id="currentImageTitle">' + title + '</div>' : '' ) + '<div id="currentImageThumbnails">'+ h + '</div>';
}
function fancyBoxJumpToImageNo(imgno){
    $.fancybox.pos(imgno);
//    alert("Jump to "+imgno+" TBI");
}
var visibleThumbnailPreview = "";
function showNavigationPreview(){
    if($(".thumb_preview").size() > 0){
        $("#l2Nav").mouseenter(function(){
            $("#mainContent").fadeTo(50,0.5);
        }).mouseleave(function(){
                $("#mainContent").fadeTo(50,1);
        });
    }

    $("#l2Nav a").mouseenter(function(){
        var tpid = "tp"+$(this).attr("id").substring(2);
        var $n = $(this);
        $(".thumb_preview").each(function(){
            if($(this).attr("id") == tpid){
                var offset = $n.offset();
                $(this).css("top", Math.floor(offset.top)+"px");
                $(this).css("left", "180px");
                visibleThumbnailPreview = "#"+tpid;
            }else{
                $(this).css("top", "-5000px");
                $(this).css("left", "-5000px");
            }
        });
    }).mouseleave(function(){
        $(visibleThumbnailPreview).css("top", "-5000px");
        $(visibleThumbnailPreview).css("left", "-5000px");
    });
}
function fancyBoxFunctionalities(){
    $(".gallery a, a.fancyBox").fancybox({
        'overlayColor'		: '#535353',
        'margin'            : 0,
        'padding'            : 0,
        'overlayOpacity'	: 0.9,
        'titlePosition' 	: 'outside',
        'titleFormat'		: formatTxtImgTitle,
        'cyclic':'true'
    });
}
function formatTxtImgTitle(title, currentArray, currentIndex, currentOpts) {
    return '<div id="fanxybox-detail-title">' + (title && title.length ?  title  : '') + '</div>';
}
function addParameterToAvoidCache(){
/* append url parameter to avoid mgnl cache */
    $(".kclink").each(function(){
        var $l = $(this).attr("href");
        if($l.indexOf("?")==-1){
            $l+="?";
        }else{
            $l+="&";
        }
        $l+="kc"+new Date().getTime();
        $(this).attr("href", $l)
    });
}

$.extend({
  getUrlVars: function(){
    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('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});
/* decryp addresses*/
function dcmadr(nnnn){
    var a = "";
    for(i=0,m=nnnn.length;i < m;i++){
        if(i%3==0){
            a += String.fromCharCode(nnnn.substr(i, 3));
        }
    }
    location.href=(a);
}

Array.prototype.shuffle = function( b ) {
 var i = this.length, j, t;
 while( i ) {
  j = Math.floor( ( i-- ) * Math.random() );
  t = b && typeof this[i].shuffle!=='undefined' ? this[i].shuffle() : this[i];
  this[i] = this[j];
  this[j] = t;
 }
 return this;
};
function get_radio_value(formhandle){
    var r = "";
    for (var i=0; i < formhandle.length; i++){
        if (formhandle[i].checked){
            r = formhandle[i].value;
        }
    }
    return r;
}

