//------------------------------------------------------------------------------
//Nordea Page Initiator Class
//Code history:
//Oct 2007: Michał Nawrot - Original code autor

function PageInitiator(){
  //------------------
  //konstruktor
  
    if(jQuery("#content").height() > 800 && jQuery("#index").size() == 0)
  	  jQuery("#backToTopLink").css("visibility", "visible");//show();
    else
  	  jQuery("#backToTopLink").css("visibility", "hidden");//hide();
  
	//------------------
	//top menu initiator
	this.initTopMenu = function(elemId){
    jQuery("#"+elemId+" > li.login, #"+elemId+" > li.contact, #"+elemId+" > li.groups").each(
      function(i){
        
        var div = jQuery(this).find("li:first").before("<div class=\"tmbox\"></div>");
        jQuery(this).find("div").css("width", jQuery(this).width()+"px");
        if(jQuery(this).children("ul").width() < jQuery(this).width())
                jQuery(this).children("ul").css("width", (jQuery(this).width()+13)*1+"px"); 
        
        
        jQuery(this).hover(      
          function(event){                
              this.className += " mover";
              
              jQuery(this).children("ul").show(200);    
                         
          },            
          function(event){      
              this.className = this.className.substr(0,this.className.indexOf(' '));
              jQuery(this).children("ul").hide();
              var self = this;
              function tout(){
                if(self.className != "mover")
                  jQuery(self).children("ul").hide();
              }
              setTimeout(tout,200);
          }
        );
      }
    );
  }
  
  //--------------------
  //left menu initiator  
  this.initLeftMenu = function(elemId){
    jQuery("#"+elemId+" li").each(
      function(i){
        jQuery(this).css("z-index", (10000-i));
      }
    );
    jQuery("#"+elemId+" li.li_out").hover(      
      function(event){
          if(jQuery(this).parent().is("#"+elemId+"")) {
          	if ( $.browser.msie) {
          		if(parseInt($.browser.version,10) == 6) {
          			jQuery(".central-col select").css("visibility","hidden");
          		}
            }
          }
          this.className = "li_over";
          jQuery(this).children("ul").show(200);    
          
      },            
      function(event){
          if(jQuery(this).parent().is("#"+elemId)) {
          	if ( $.browser.msie) {
          		if(parseInt($.browser.version,10) == 6) {
          			jQuery(".central-col select").css("visibility","visible");
          		}
            }
          }
          this.className = "li_out";
          jQuery(this).children("ul").hide();
          var self = this;
          function tout(){
            if(self.className != "li_over")
              jQuery(self).children("ul").hide();
          }
          setTimeout(tout,200);
      }
    );
    jQuery("#"+elemId+" li.li_out_end").hover(      
      function(event){
          this.className = "li_over_end";
      },          
      function(event){        
          this.className = "li_out_end";
      }
    );
  }
  
  this.initLinks = function(){
	  jQuery("a.target_blank").each(
		  function(i){
			this.target="_blank";
		  }
	  );
  }
  
}

//------------------------------------------------------------------------------
//on document is loaded
jQuery(document).ready(
  function(){
  
	var w = $('#content').width();
    $('#content').find('table').each(function(){
        var tw = $(this).width();
        while(tw>w){
            var orgFontSize = $(this).find('td').css('font-size');
            if(orgFontSize.indexOf("px") != -1){
                var fontSize = parseInt(orgFontSize);
                --fontSize;
                $(this).find('td').css('font-size', fontSize);
                //console.log('-1/'+fontSize);
            }
            tw = $(this).width();
        }
    });
	
    initiator = new PageInitiator();
    initiator.initLeftMenu("main_category_menu");
    initiator.initTopMenu("toolbar");      
    initiator.initLinks();
    if(jQuery.browser.msie){
      var ieVersion = parseFloat(jQuery.browser.version);
      if( ieVersion >= 5.5 &&  ieVersion < 6){
        jQuery("#wrapper").wrap("<center></center>");
        jQuery("link").each(
          function(i){
            if(this.href == "_items/css/screen.css")
              this.href = "_items/css/screen_ie55.css";
            if(this.href == "_items/css/index.css")
              this.href = "_items/css/index_ie55.css";
          }
        )
      }
    }
  }
);


function ChangeColor(tableRow, highLight){
    if (highLight)
    {
      $(tableRow).find('td').each(function() {
	$(this).css('background-color', '#D3D3D3');
      });
      tableRow.style.cursor="hand";
    }
    else
    {
      $(tableRow).find('td').each(function() {
	if($(this).parent().hasClass('odd')) $(this).css('background-color', '#fff');
	else $(this).css('background-color', '#F1F1F1');
      });
      tableRow.style.cursor="pointer";
    }
}

function doNav(url){
  document.location.href = url;
}


	
var emailTmpVal = 'E-mail';
var contentTmpVal = 'Wpisz swoje pytanie...';

var sendEmailSuccessAlert = 'Dziękujemy. Wiadomość została wysłana.';
var sendEmailErrorAlert = '<span style="color: #f44;">Błąd przy wysyłaniu wiadomości.</span>';

var mailid = 0;
var serviceemailid = 0;


function resetUserFields() {
	$('.email-tmp-val').val(emailTmpVal);
	$('.content-tmp-val').val(contentTmpVal);
	$('.cf-alert').html('');
}


function validateForm(formId) {
	var result = true;
	if(validateEmail($("input[name=p_kontakt_email]", "#"+formId).val())) {
		$("input[name=p_kontakt_email]", ("#"+formId)).css("background-color", "#fcc");
		result = false;
	} else {
		$("input[name=p_kontakt_email]", ("#"+formId)).css("background-color", "#fff");
	}
	//alert($("textarea[name=p_kontakt_tresc]", ("#"+formId)).val());
	if($("textarea[name=p_kontakt_tresc]", ("#"+formId)).val().length == 0 || $("textarea[name=p_kontakt_tresc]", ("#"+formId)).val() == "Treść zapytania" || $("textarea[name=p_kontakt_tresc]", ("#"+formId)).val() == "Your inquiry") {
		$("textarea[name=p_kontakt_tresc]", ("#"+formId)).css("background-color", "#fcc");
		result = false;
	} else {
		$("textarea[name=p_kontakt_tresc]", ("#"+formId)).css("background-color", "#fff");
	}
	return result;
}

function validateEmail(elementValue){
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	return !emailPattern.test(elementValue);
}

(function($) {

	$.fn.easySlider = function(options){
	  
		// default configuration properties
		var defaults = {			
			prevId: 		'prevBtn',
			prevText: 		'',
			nextId: 		'nextBtn',	
			nextText: 		'',
			controlsShow:	true,
			controlsBefore:	'',
			controlsAfter:	'',	
			controlsFade:	true,
			firstId: 		'firstBtn',
			firstText: 		'First',
			firstShow:		false,
			lastId: 		'lastBtn',	
			lastText: 		'Last',
			lastShow:		false,				
			vertical:		false,
			speed: 			800,
			auto:			true,
			pause:			8000,
			continuous:		true, 
			numeric: 		false,
			numericId: 		'controls'
		}; 
		
		var options = $.extend(defaults, options);  
				
		this.each(function() {  
			var obj = $(this); 				
			var s = $("li", obj).length;
			var w = $("li", obj).width(); 
			var h = $("li", obj).height(); 
			var clickable = true;
			obj.width(w*4); 
			obj.height(); 
			obj.css("overflow","hidden");
			var ts = s-1;
			var t = 0;
			$("ul", obj).css('width',s*w);			
			
			if(options.continuous && s>4){
				$("ul", obj).prepend($("ul li:last-child", obj).clone().css("margin-left","-"+ w +"px"));
				$("ul", obj).append($("ul li:nth-child(2)", obj).clone());
				$("ul", obj).append($("ul li:nth-child(3)", obj).clone());				
				$("ul", obj).append($("ul li:nth-child(4)", obj).clone());
				$("ul", obj).append($("ul li:nth-child(5)", obj).clone());
				$("ul", obj).css('width',(s+1)*w*4);
			};				
			
			if(!options.vertical) $("li", obj).css('float','left');
								
			if(options.controlsShow && s>4){
				var html = options.controlsBefore;				
				if(options.numeric){
					html += '<ol id="'+ options.numericId +'"></ol>';
				} else {
					html += ' <div id="buttons"><span id="'+ options.prevId +'"><a href=\"javascript:void(0);\"><img src="img/leftmenu_arrow_p.gif"  class="right prev" alt="Poprzedni" />'+ options.prevText +'</a></span>';
					if(options.lastShow) html += ' <span id="'+ options.lastId +'"><a href=\"javascript:void(0);\">'+ options.lastText +'</a></span>';
					if(options.firstShow) html += '<span id="'+ options.firstId +'"><a href=\"javascript:void(0);\">'+ options.firstText +'</a></span>';
					html += ' <span id="'+ options.nextId +'"><a href=\"javascript:void(0);\"><img src="img/leftmenu_arrow.gif"  class="right next" alt="Następny" />'+ options.nextText +'</a></span></div>';
				};
				
				html += options.controlsAfter;						
				$(obj).after(html);										
			};
			
			if(options.numeric){									
				for(var i=0;i<s;i++){						
					$(document.createElement("li"))
						.attr('id',options.numericId + (i+1))
						.html('<a rel='+ i +' href=\"javascript:void(0);\">'+ (i+1) +'</a>')
						.appendTo($("#"+ options.numericId))
						.click(function(){							
							animate($("a",$(this)).attr('rel'),true);
						}); 												
				};							
			} else {
				$("a","#"+options.nextId).click(function(){		
					animate("next",true);
				});
				$("a","#"+options.prevId).click(function(){		
					animate("prev",true);				
				});	
				$("a","#"+options.firstId).click(function(){		
					animate("first",true);
				});				
				$("a","#"+options.lastId).click(function(){		
					animate("last",true);				
				});				
			};
			
			function setCurrent(i){
				i = parseInt(i)+1;
				$("li", "#" + options.numericId).removeClass("current");
				$("li#" + options.numericId + i).addClass("current");
			};
			
			function adjust(){
				if(t>ts) t=0;		
				if(t<0) t=ts;	
				if(!options.vertical) {
					$("ul",obj).css("margin-left",(t*w*-1));
				} else {
					$("ul",obj).css("margin-left",(t*h*-1));
				}
				clickable = true;
				if(options.numeric) setCurrent(t);
			};
			
			function animate(dir,clicked){
				if (clickable){
					clickable = false;
					var ot = t;				
					switch(dir){
						case "next":
							t = (ot>=ts) ? (options.continuous ? t+1 : ts) : t+1;						
							break; 
						case "prev":
							t = (t<=0) ? (options.continuous ? t-1 : 0) : t-1;
							break; 
						case "first":
							t = 0;
							break; 
						case "last":
							t = ts;
							break; 
						default:
							t = dir;
							break; 
					};	
					var diff = Math.abs(ot-t);
					var speed = diff*options.speed;						
					if(!options.vertical) {
						p = (t*w*-1);
						$("ul",obj).animate(
							{ marginLeft: p }, 
							{ queue:false, duration:speed, complete:adjust }
						);				
					} else {
						p = (t*h*-1);
						$("ul",obj).animate(
							{ marginTop: p }, 
							{ queue:false, duration:speed, complete:adjust }
						);					
					};
					
					if(!options.continuous && options.controlsFade){					
						if(t==ts){
							$("a","#"+options.nextId).hide();
							$("a","#"+options.lastId).hide();
						} else {
							$("a","#"+options.nextId).show();
							$("a","#"+options.lastId).show();					
						};
						if(t==0){
							$("a","#"+options.prevId).hide();
							$("a","#"+options.firstId).hide();
						} else {
							$("a","#"+options.prevId).show();
							$("a","#"+options.firstId).show();
						};					
					};				
					
					if(clicked) clearTimeout(timeout);
					if(options.auto && s>4){;
				timeout = setTimeout(function(){
					animate("next",false);
				},options.pause);
			};	
			
				};
				
			};
			// init
			var timeout;
			if(options.auto && s>4){;
				timeout = setTimeout(function(){
					animate("next",false);
				},options.pause);
			};		
			
			if(options.numeric) setCurrent(0);
		
			if(!options.continuous && options.controlsFade){					
				$("a","#"+options.prevId).hide();
				$("a","#"+options.firstId).hide();				
			};				
			
		});
	  
	};

})(jQuery);
