/**
 * @author Kalin Steafnov
 * wizzard.bg
 */

$(document).ready(function(){
    if($('#slider').length > 0) {
    	$('#slider ul a img').click(function() {
    		var temp = $(this).attr('src');
    		var id = $(this).parent().attr('id');
    		temp = temp.replace(/([0-9]+)_thumb106x96/, id)

    		var main_image = $('#mainImgCont p.gallery img');

    		main_image.fadeOut('fast', function() {

    			if($.browser.msie)
    				temp = temp + '?random=' + (new Date()).getTime();

    			var new_image = new Image();
    			new_image.src = temp;


    			$(new_image).load(function() { main_image.attr('src', temp).fadeIn() })
    		});
    	})
	}

	//Second navigation
	$('ul.navigation li').hover(
	    function() {
	    $(this).children('ul').show();
	    },
	    function() {
	        $(this).children('ul').hide();
	    }
	);

	if($('.open_filter').length > 0) {
        var is_init_ctm_sel = false;
	    $('.open_filter').click(function() {
	        if($('#filter_popup').hasClass('opened'))
	           $('#filter_popup').hide().removeClass('opened');
	        else {
	           $('#filter_popup').show().addClass('opened');
               if(!is_init_ctm_sel) {
                    init_custom_select();
                    is_init_ctm_sel = true;
                }
            }
	    })

	    $('#filter_popup .close_button').click(function() {

	    	// Clear regular form fields
			$(':input', $('#buyback_filter')).each(function() {
				var type = this.type;
				var tag = this.tagName.toLowerCase(); // normalize case
				if (type == 'text' || type == 'password' || tag == 'textarea')
					this.value = "";
				else if (type == 'checkbox' || type == 'radio')
					this.checked = false;
				else if (tag == 'select')
					this.selectedIndex = -1;
			});

			// Clear custom form fields (Wizzard.CustomSelect)
			$('.ctm_content').each(function() {
				$(this).html('<span style="width:' + $(this).width() + 'px">Всички</span>');
				$(this).trigger('change');
			});

			var f = false;

			// Clear real select form fields (used in Wizzard.CustomSelect)
			$('.Wizz_sel').each(function() {
				$(this).children().each(function() {
					$(this).removeClass('hover');
					f = true;
				});
				f = false;
				// $(this).trigger('change');
			});

			if (f)
				$('#other_model').hide();

	        $('#filter_popup').hide().removeClass('opened');

	    });

	    $('#filter_drag_cnt').mousedown(function(e) {
	        if (e.preventDefault) {
                  e.preventDefault();
              }

	        var box = $('#filter_popup');
	        var box_left = parseInt(box.css('left'));
	        var box_top = parseInt(box.css('top'));

	        var x_pos = e.pageX;
	        var y_pos = e.pageY;
	        $(window.document).mousemove(function(ee) {
                box.css('left', box_left + (ee.pageX-x_pos))
                box.css('top', box_top + (ee.pageY-y_pos))
	        })
	    })

        $('#filter_drag_cnt').mouseup(function() {
            $(window.document).unbind('mousemove');
        })

         $('.open_filter').click();
	}
})

