﻿$(function() {
   $.extend({
     CBZ_FORM : {
      setupFormValidationFields: function(obj){
        $('#'+obj.formId).validate({focusInvalid: false});
        var frm = $( '#'+obj.formId );
        $( 'input', frm ).add('textarea',frm).each( function(){
          
          if( this.type != 'hidden' ) {
            
            var inputEl = $(this);
            var fieldName = this.name;              
            var siblings = $(this).siblings();
            
            $(siblings).filter('.mssg').each(function(){
              if($(this).html()=='*')
                $( inputEl ).addClass( "required" ); 
            });
            if(fieldName.indexOf("mail") != -1 && obj.mail)
              $( this ).addClass( "email" );
            if(fieldName.indexOf("url") != -1 && obj.url)
              $( this ).addClass( "url" );
          }
        });
      },
      setupTooltips: function(settings){
        var tooltips = settings.tooltips;
        for(var i in tooltips){
          if (typeof tooltips[i] == 'string') {
            var arr = tooltips[i].split('::');
            
            $('#'+arr[0]).attr('title',arr[1]).tooltip({
              position: settings.position.position,
              offset: settings.position.offset,
              effect: settings.position.effect,
              opacity: settings.position.opacity
            });
          }
        }
      }
    },
    
     UGH :{    
      infobox_pointer : '',
      init : function(){
        $(".overlay").mouseenter(function(){ $.UGH.clearHideInfoboxTimer();})
                     .mouseleave(function(){$.UGH.delayInfoboxHiding();});
        
        $("#Topbillede").mouseleave(function(){$.UGH.delayInfoboxHiding();});
     
        $.UGH.x = ($("#Topbillede").offset()).left;
        $.UGH.y = ($("#Topbillede").offset()).top;
      },
      showInfobox : function(el, deptObj){
        // FIRST CLOSE ANY BOXES THAT ARE ALREADY OPEN
        //$.UGH.clearHideInfoboxTimer();
        $.UGH.hideInfoboxNow();
        
        $.UGH.infoboxEl = $(el).attr("id") == "_4" ? ".city-depts-multi" : ".city-depts";
        
        $.UGH.updateInfobox(el, deptObj);
        
        $( $.UGH.infoboxEl ).css({position : "absolute", top: $.UGH.y, left:$.UGH.x});
        $( $.UGH.infoboxEl ).fadeIn(150);//.animate({height: "show"}, 100, "linear"); 
      },
      delayInfoboxHiding : function(){
         // HIDE ONLY IF USER DOESN'T MOVE MOUSE TO THE INFOBOX WITHIN 2 SECONDS
         $.UGH.infobox_timer = setTimeout('$.UGH.hideInfoboxNow()', 1500);
      },
      clearHideInfoboxTimer: function(){
        clearTimeout( $.UGH.infobox_timer );
      },
      hideInfoboxNow : function(){
        $( $.UGH.infoboxEl ).fadeOut(150);
      },
       
      updateInfobox : function(el, o){
        $($.UGH.infoboxEl).find("h2").html(o.city);
        $($.UGH.infoboxEl).find("a").attr("href", o.url);
        if($(el).attr("id") == "_4"){ // if Frh.
          for(var i = 0; i < o.departments.length; i++){
            var dept = o.departments[i];
            $(".city-wrapper").eq(i).find("dt").find("a").html(dept.street);
            $(".city-wrapper").eq(i).find("dd").find("a").html(dept.models);
            $(".city-wrapper").eq(i).find("img").attr("src", dept.imagesrc);
          }
        }else{
          $($.UGH.infoboxEl).find("dt").find("a").html(o.street);
          $($.UGH.infoboxEl + " .models").find("a").html(o.models);
          $($.UGH.infoboxEl).find("img").attr("src",o.imagesrc);
        }
      }
    }
   });
});

function cleartextbox(element)
{
  element.value = '';
}

function filltextbox(element,text,allowifhascontent)
{
  if (element.value.length > 0)
  {
    if (allowifhascontent)
    {
      element.value = text;
    }
  }
  else
  {
    element.value = text;
  }
}
     
function getURL( 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];
}


var myPopupWindow = '';
function openPopupWindow(url, name, width, height) {
  //Remove special characters from name
  name = name.replace(/\/|\-|\./gi, "");
   
  //Remove whitespaces from name
  var whitespace = new RegExp("\\s","g");
  name = name.replace(whitespace,"");
  
  //If it is already open
  if (!myPopupWindow.closed && myPopupWindow.location) {
    myPopupWindow.location.href = encodeUrl(url);
  }
  else {
    myPopupWindow= window.open(encodeUrl(url),name, "location=no, scrollbars=yes, resizable=yes, toolbar=no, menubar=no, width=" + width + ", height=" + height );
    if (!myPopupWindow.opener) myPopupWindow.opener = self;
  }
   
  //If my main window has focus - set it to the popup
  if (window.focus) {myPopupWindow.focus()}
}

function encodeUrl(url) {
  if (url.indexOf("?")>0) {
    encodedParams = "?";
    parts = url.split("?");
    params = parts[1].split("&");
    for(i = 0; i < params.length; i++) {
      if (i > 0) {
        encodedParams += "&";
      }
      if (params[i].indexOf("=")>0) {
        p = params[i].split("=");
        encodedParams += (p[0] + "=" + escape(encodeURI(p[1])));
      }
      else {
        encodedParams += params[i];
      }
    }
    url = parts[0] + encodedParams;
  }
  return url;
}
