var ceilColor = $('#naviBox .ceil .color li');
var showAll   = $('#naviBox .showAll');
var checkBox  = $('#naviBox input[type=checkbox]');
var select    = $('#naviBox select');
var resetBnt  = $('#naviBox .reset');
var keywords  = $('#keywords');
var naviForm  = $('#naviForm');
var sortby    = $('#mainBox select');
var viewby    = $('#mainBox .rpage .viewby');
var viewImg   = $('#mainBox .rpage .viewImg');
var page      = $('#mainBox .ppage a');
var init      = $('#init');
var uri       = $('#uri').val();

var checkKeywords = function() {
        var s = $('#keywords').val();
        return ('' != s) ? 'keywords=' + s : '';
};

var checkGender = function() {
        var s = $('#schGender').val();
        return ('' != s) ? 'gender_id=' + s : '';
};

var checkCataIds = function() {
        var s = '';

        $('#naviBox .cataIds').each(function(i) {
                if (this.checked) s += (('' == s) ? '' : ',') + $(this).val();
        });

        return ('' != s) ? 'categories_id=' + s : '';
};

var checkBifocal = function() {
        var s = '';

        $('#schBifocal').each(function(i) {
                if (this.checked) s = 'products_bifocal=1';
        });

        return s;
};

var checkVideo = function() {
        var s = '';

        $('#schVideo').each(function(i) {
                if (this.checked) s = 'schVideo=1';
        });

        return s;
};

var checkSize = function() {
        var s = $('#schWidth').val();
        return ('' != s) ? 'schWidth=' + s : '';
};

var checkPrice = function() {
        var s = $('#schPrice').val();
        return ('' != s) ? 'p_price_range_ids=' + s : '';
};

var checkSortby = function() {
        var s = $('#mainBox select').val();
        return ('' != s && 'undefined' != s) ? 'sortby=' + s : '';
};

var checkColor = function() {
        var s = $('#schColor').val();
        return ('' != s) ? 'schColor=' + s : '';
}

var checkAll = function() {
        var s = '';
        var _t = '';

        if (_t = checkKeywords()) {
                s += (('' == s) ? '' : '&') + _t;
        }
        if (_t = checkGender()) {
                s += (('' == s) ? '' : '&') + _t;
        }
        if (_t = checkCataIds()) {
                s += (('' == s) ? '' : '&') + _t;
        }
        if (_t = checkBifocal()) {
                s += (('' == s) ? '' : '&') + _t;
        }
        if (_t = checkVideo()) {
                s += (('' == s) ? '' : '&') + _t;
        }
        if (_t = checkSize()) {
                s += (('' == s) ? '' : '&') + _t;
        }
        if (_t = checkPrice()) {
                s += (('' == s) ? '' : '&') + _t;
        }
        if (_t = checkSortby()) {
                s += (('' == s) ? '' : '&') + _t;
        }
        if (_t = checkColor()) {
                s += (('' == s) ? '' : '&') + _t;
        }

        return s;
};

/************** todo ************/
var getLst = function(s) {

	var s1 = $('#keywords').val();
        if ('' != s1) {
		window.location.href = '/search.php?keywords=' + s1 ;

	}else{
		//$("#mainBox").lock('<img src="style/images/ajax-loader.gif" />', null, {backgroundColor: "#ffffff", opacity: "0.6"});

		/*var strUrl = location.href;
		var timenow = new Date().getTime();
		if (strUrl.indexOf("#") > 0) {
			location.href = strUrl.substring(strUrl, strUrl.indexOf("#")) + "#t=" + timenow;
		} else {
			location.href = location.href + "#t=" + timenow;
		}*/

		var param = (typeof(s) == 'undefined') ? '' : s;

		if ('1' == init.val()) {
			var href = uri;
			var check = checkSortby();
			if (check) {
				param += ((param) ? '&' : '') + check;
			}
		} else {
			var href = 'samplenew.php';
			var check = checkAll();
			if (check) {
				param += ((param) ? '&' : '') + check;
			}
		}

		var prefix = (href.indexOf('?') > 0) ? '' : '?';

		window.location.href = href + prefix + param;
        }
        /*
        $.get(
                href + prefix + param,
                function(data){
                        if ('1' != init.val()) {
                                $('#cataBox').hide();
                                $('#helpBarSliver').css('top', 42);
                                $('#helpBarFrame').css('top', 42);
                        }

                        $("#mainBox").html( data );

                        $(document).pngFix2();

                        //function live didn't support change action
                        $("#mainBox select").bind('change', function() {
                                getLst();
                        });
                }
        );
        */
};

var reset = function() {
        $('#keywords').val('');
        $('#schGender').val('');
        $('#naviBox .cataIds').each(function(i) {
                this.checked = false;
        });
        $('#schBifocal').each(function(i) {
                this.checked = false;
        });
        $('#schVideo').each(function(i) {
                this.checked = false;
        });
        $('#schColor').val('');
        $('#schWidth').val('');
        $('#schPrice').val('');
};

/************** Action ***********/
showAll.click(function() {
        init.val('0');
        reset();
        getLst();
});

checkBox.click(function() {
        init.val('0');
        getLst();
});

resetBnt.click(function() {
        init.val('0');
        reset();
});

select.change(function() {
        init.val('0');
        getLst();
});

keywords.change(function() {
        init.val('0');
        getLst();
});

naviForm.submit(function() {
        init.val('0');
        getLst();
        return false;
});

sortby.change(function() {
        getLst();
});

viewby.live('click', function() {
        var n = $(this).html();
        var s = 'perPage=' + ((isNaN(n)) ? '-1' : n);
        getLst(s);
});

viewImg.live('click', function() {
        var s = 'imgView=' + $(this).attr('alt');
        getLst(s);
});

page.live('click', function() {
        var s = 'pg=' + $(this).html();
        getLst(s);
        return false;
});

/************ sideBox ***********/
var topH  = $('.wrapper .header').outerHeight() + $('.wrapper #header').outerHeight() + $('.wrapper #navi').outerHeight() + $('.wrapper #naviBox h3').outerHeight() + parseInt($('.container').css('padding-top')) + topHadd;
var navi  = $('#naviBox');
var naviH = navi.outerHeight();
var padH  = parseInt($('.container').css('padding-top')) + parseInt($('.container').css('padding-bottom'));

var step   = 10;
var bStep  = 100;

var naviScroll = function() {
        var docuSH = $(document).scrollTop();
        if (docuSH > topH) {
                var maxH   = $('.container').outerHeight() - padH - naviH;
                var naviCT = parseInt(navi.css('top'));
                var differ = docuSH - topH - naviCT;
                var suffix = (differ > 0) ? 1 : -1;
                var movePx = 0;

                differ = Math.abs(differ);
                if (differ > bStep) {
                        movePx = bStep * suffix + naviCT;
                } else {
                        movePx = (differ < step ? differ : step) * suffix + naviCT;
                }
                navi.css('top', movePx > maxH ? maxH : movePx);
        } else {
                navi.css('top', 0);
        }

        s = setTimeout("naviScroll();", 100);
};


/* fashion page*/
function showtips()
{
	$(".fashion-tips").show();
	$(".fashion-articles").hide();
	$(".fashion-photos").hide();

}
function showphotos()
{
	$(".fashion-photos").show();
	$(".fashion-articles").hide();
	$(".fashion-tips").hide();

}

function showarticles(){
	$(".fashion-photos").hide();
	$(".fashion-articles").show();
	$(".fashion-tips").hide();
}

function showprev()
{
	//$(".fashion-face2").hide();
	//$(".fashion-face1").show();


	$(".fashion-face2").animate({"left": "-920px"}, "slow");
	$(".fashion-face1").animate({"left": "0px"}, "slow");

}

function shownext()
{
	//$(".fashion-face1").hide();
	//$(".fashion-face2").show();

	//$(".fashion-face2").css("left": "-874px");
	$(".fashion-face1").animate({"left": "920px"}, "slow");
	$(".fashion-face2").animate({"left": "0px"}, "slow");
}

/* fashion page end */