var cnt = 0;
var cnt_max;



var local = {};



$(function(){
    $("input.placeholder").watermark({
        observer: 'watermark_login'
    });
    
    
      EarsSlider.init({
      	source: $("#ib_content_list"),
      	width: '965',
      	delay: 8000
      }, 
      {
      	source: $("#ib_content_slider"),
      	anim_time: 500
      });  
});



var EarsSlider = (function() {
	var slides = {
		source: null,
		count: 0,
		width: 0,
		current: 0,
		delay: 1000,
		first_delay: 200,
		timer: false,
		is_over: false
	};
	var slider = {
		source: null,
		anim_time: 200
	}
	
	return {
		init: function(slides, slider) {
			this.slides = slides
			this.slides.count = $("li", this.slides.source).size();
			this.slides.current = 0;
			this.slider = slider;
			
			this.binding();
			this.autoPlay(this.slides.current, true);
		},
		binding: function() {
			$("li > a", this.slider.source).live('click', function() {
				EarsSlider.moveTo((parseInt($(this).html()) - 1));
				
				return false;
			});
			
			this.slides.source.parent().hover(
				function() {
					EarsSlider.slides.is_over = true;
					clearTimeout(EarsSlider.slides.timer);
				},
				function() {
					EarsSlider.slides.is_over = false;
					EarsSlider.autoPlay(EarsSlider.slides.current + 1);
				}
			);
			
			$("li", this.slides.source).live('click', function() {
				window.location = $("h2 > a", this).attr("href");
			});
		},
		moveTo: function(step) {
			var offset = -step * EarsSlider.slides.width + "px";						
			var flag = false;

			if(step == EarsSlider.slides.count) {
				EarsSlider.slides.current == 0;
				step = 0;
				flag = true;
			}		
			else {
				flag = false;
			}		
			EarsSlider.slides.source.animate({
				left: offset
			}, EarsSlider.slider.anim_time, function() {
				if(flag)
				{
					$("li.added", EarsSlider.slides.source).remove();
					EarsSlider.slides.source.css('left', '0');
				}				
			});			
			
			EarsSlider.aToSpan(step);
			
			var next = this.slides.current + 1;
			
			if(next == this.slides.count) {
				$("li:eq(0)", EarsSlider.slides.source).clone().addClass("added").appendTo(EarsSlider.slides.source);
			}
			
			if(!EarsSlider.slides.is_over) {
				EarsSlider.autoPlay(next, false);	
			}	
		},
		autoPlay: function(from, first) {
			var delay = first == true ? EarsSlider.slides.first_delay : EarsSlider.slides.delay;

			this.slides.timer = setTimeout(function() {
				EarsSlider.moveTo(from);
			}, delay);
		},
		aToSpan: function(step) {
			var li = $("li:eq(" + this.slides.current + ")", this.slider.source);
			var li_span_html = this.slides.current == 0 ? '1' : $("span", li).html(); 
			
			li.empty().append('<a href="#">' + li_span_html + '</a>');
			this.slides.current = step;
			
			var li = $("li:eq(" + this.slides.current + ")", this.slider.source);
			var li_a = {
				html: $("a", li).html()
			}	
			
			li.empty().append('<span>' + li_a.html + '</span>');		
		}
	}
})();
    	


$(document).ready(function(){

    search_text = $("#search").val();
    $("#login_link").click(function(){
        $("#login_box").show();
    }, function(){
        $("#login_box").hide();
    });
    
    
    $("#weather_city").click(function(){
        $("#weather_select").show();
		return false;
    })


	
    $("#weather_select").hover(function(){
    }, function(){
        $("#weather_select").hide();
    }).click(function(){
        $("#weather_select").hide();
    })
    
    $(".errorSummary").prepend($('<a href="#" class="close"></a>').click(function(){
        $(this).parent().hide();
        return false;
    }));
    
    


    
});




function moveMenu(){
    if (parseInt($("#menu_in").css('left')) == 0) {
        $("#menu_in").stop().animate({
            left: -1 * parseInt($("#menu_in").css('width')) + 965
        }, 500, null);
        $("#menu_outer").addClass('moved');
    }
    else {
        $("#menu_in").stop().animate({
            left: 0
        }, 500, null);
        $("#menu_outer").removeClass('moved');
    }
}

function InputTextInfo(inputClass, active_class){
    $("." + inputClass).each(function(){
        local[$(this).attr("name")] = $(this).val();
        $(this).focus(function(){
            if ($(this).val() == local[$(this).attr("name")]) {
                $(this).val("");
                $(this).addClass(active_class);
            }
        });
        $(this).blur(function(){
            if ($(this).val() == "") {
                $(this).val(local[$(this).attr("name")]);
                $(this).removeClass(active_class);
            }
        });
    })
}


function isEmail(str){
    var result1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
    var result2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,8}|[0-9]{1,3})(\\]?)$");
    return (!result1.test(str) & result2.test(str));
}

function movePageTo(e, pos, duration){
    if (e) {
        scroll_val = $(e).offset().top + (pos != 'top' ? $(e).height() - $("body").height() + 1 : 0);
        scrolltop = $("body").scrollTop() || $("html").scrollTop();
        if (scrolltop < scroll_val) 
            $("html:not(:animated),body:not(:animated)").animate({
                scrollTop: scroll_val
            }, (duration ? duration : 500));
    }
    else {
        $("html:not(:animated),body:not(:animated)").animate({
            scrollTop: pos + 'px'
        }, (duration ? duration : 500));
    }
}

function showLogin(){
    $('#registration_box').hide();
    $('#login_box').slideDown();
}

function showRegistration(){
    $('#login_box').hide();
    $('#registration_box').slideDown();
}

    (function($) {

        select_num = 0;


        $.yuSelect = function(this_el, options) {

            select_num++;
            var thisObj = this;

            def_options = {
                width:'fix'
            }


            this.init = function init(options) {

                var append = "";

                this.options = $.extend(def_options, options);


                $("option", this_el).each(function() {
                    append += '<a href="#" value="' + $(this).attr('value') + '"' + ($(this).attr('class') ? ' class="' + $(this).attr('class') + '"' : '') + '>' + $(this).html() + '</a>';
                });


                this.s_name = $(this_el).attr('name').toString().replace(/\[|\]/g, "");
                this.inputId = $(this_el).attr('id');
                this.selected = $(this_el).find("option:selected");

                this.id_s = this.s_name + '_' + parseInt(Math.random() * 1e10);

                if (this.options.width == 'fix') {
                    width = $(this_el).width() + 'px';
                }
                else {
                    width = this.options.width;
                }


                $(this_el).replaceWith('<div class="select_view fl" style="width:' + width + ';z-index:' + (100 - select_num) + ';" id="select_' + this.id_s + '"><input type="hidden" name="' + $(this_el).attr('name') + '" value="' + $(this.selected).attr('value') + '" id="' + $(this_el).attr('id') + '"/><div class="butt"></div><div class="now_act">' + $(this.selected).html() + '</div><div class="drop_box">' + append + '</div></div>');


                this.this_box = $("#select_" + this.id_s);


                $(this.this_box).click(
                        function() {
                            $(this).find(".drop_box").slideDown(300, function(e) {
                                movePageTo($(this));
                            });
                        }).hover(function() {
                    sphere_hover = true;
                }, function() {
                    sphere_hover = false;
                    $this = this;
                    setTimeout(function() {
                        if (!sphere_hover)
                            $($this).find(".drop_box").slideUp(300);
                    }, 600);
                });


                $(this.this_box).find(".drop_box a").click(thisObj.select_click);
                $("body").trigger("initSelect", [thisObj.inputId, $(this.selected).attr('value')]);

            }
            this.select_click = function() {

                $(thisObj.this_box).find(".now_act").html($(this).html());
                $(thisObj.this_box).find(".drop_box").hide();
                $(thisObj.this_box).find("input").val($(this).attr('value'));

                $("#" + thisObj.inputId).trigger("changeValue", [$(this).attr('value')]);

                return false;
            }

            this.init(options);

        }


        $.fn.yuSelect = function(options) {
            this.each(function() {
                if (this.tagName == "SELECT")
                    new $.yuSelect(this, options);
            });

            return this;
        };


    }(jQuery));

