﻿function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else  expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(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;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

;(function( $ ) {

	
	$.extend({
        equalHeight: function() {
            $('.EqualHeight').children().height('auto');
            $('.EqualHeight').each(function(){
                var maxHeight = 0;
                $(this).children().each(function(){
                    if($(this).height() > maxHeight ){
                        maxHeight = $(this).height();
                    }
                });
                $(this).children().height(maxHeight);
            });
        },
		
		dd_widthSet: function(){
			if($('.dd_holder').length) {
				$('.dd_holder').each(function(){
					$(this).find('ul.clearfix').width($(this).find('ul.clearfix > li').outerWidth(true)*$(this).find('ul.clearfix > li').length);
				});
			}
		},
		
		inputtextValuesSet: function() {
			$('input[type="text"], textarea').each(function(){
				if($(this).attr('title') && $.fTrim($(this).val()).length === 0 ){
					$(this).val($(this).attr('title'));
				}
				
				$(this).blur(function(){
					var sValue = $.fTrim(this.value);
					if(sValue.length===0){
						$(this).val($(this).attr('title'));
					}
				});
				
				$(this).focus(function(){
					var sValue = $.fTrim(this.value);
					if(sValue==$(this).attr('title')){
						this.value="";
					}
				});
			});
			
		
			$('.sidebar_form input[type="password"], .sidebar_form input[type="text"]').each(function(i){
				if($.fTrim($(this).val()).length !== 0 ){
					$(this).prev('label').hide();
				}
			});
			
			$('.sidebar_form input[type="password"], .sidebar_form input[type="text"]').focus(function(){
				$(this).prev('label').hide();
			});
				
			$('.sidebar_form input[type="password"], .sidebar_form input[type="text"]').blur(function(){
				var sValue=$.fTrim(this.value);
				if(sValue.length===0){
					$(this).prev('label').show();
				}
			});
			
		},
		
		fTrim: function(s){
			return s.replace(/^\s*(.*?)\s*$/,"$1");
		},
		
		cuSelApply: function() {
			var cuselParams = {
				changedEl: "select",
				visRows: 5,
				scrollArrows: true
			}
			cuSel(cuselParams);
		},
		
        scaleFont: function(){
            $(".font_controllers a.big_link").click(function(){
				$.addActive(this);
                $("body").queue(function(next){
                    eraseCookie("FontSize");
                    $("body").removeClass("MediumFont");
                    $("body").removeClass("SmallFont");
                    next();
                })
                .queue(function(next){
                    $("body").addClass("LargeFont");
                    createCookie("FontSize", "LargeFont", 30);
                    next();
                });
				return false;

            });
            $(".font_controllers a.medium_link").click(function(){
				$.addActive(this);
                $("body").queue(function(next){
                    eraseCookie("FontSize");
                    $("body").removeClass("LargeFont");
                    $("body").removeClass("SmallFont");
                    next();
                })
                .queue(function(next){
                    $("body").addClass("MediumFont");
                    createCookie("FontSize", "MediumFont", 30);
                    next();
                });
				return false;
            });
            $(".font_controllers a.small_link").click(function(){
				$.addActive(this);
                $("body").queue(function(next){
                    eraseCookie("FontSize");
                    $("body").removeClass("LargeFont");
                    $("body").removeClass("MediumFont");
                    next();
                })
                .queue(function(next){
                    $("body").addClass("SmallFont");
                    createCookie("FontSize", "SmallFont", 30);
                    next();
                }); 
				return false;
            });
        },
		
		addActive: function(el) {
			$(el).siblings().removeClass('active');
			$(el).addClass('active');
		},
		
		setUpFont: function() {
			var fs = readCookie("FontSize");
			switch(fs) {
				case "LargeFont":
					$("body").addClass("LargeFont");
					$.addActive('.font_controllers a.big_link');
					break;
				case "SmallFont":
					$.addActive('.font_controllers a.small_link');
					$("body").addClass("SmallFont");
					break;
				default:
					$.addActive('.font_controllers a.medium_link');
					$("body").addClass("MediumFont");
					break;
			}
		},
		
		faqBehavior: function() {
			if($('.faq_list').length){
				$('.faq_list li:not(.opened)').each(function(){
					$(this).addClass('def_faq');
				});
				$('.faq_list li > h4 > a').click(function(){
					$(this).parent().parent().siblings().addClass('def_faq');
					$(this).parent().parent().removeClass('def_faq');
					return false;
				});
			}
		},
		
		init: function() {
			$.equalHeight();
			$.dd_widthSet();
			$.inputtextValuesSet();
			$.cuSelApply();
			$.scaleFont();
			$.setUpFont();
			$.faqBehavior();
		}
	});
	
} )(jQuery);

jQuery.noConflict();
jQuery( function( $ ) {
	$.init();
} );

 
