var ceilColor = $('#naviBox .ceil .color li');
var showAll   = $('#naviBox .showAll');
var checkBox  = $('#naviBox input[type=checkbox]');
var LensCheckbox  = $('.lens 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 checkLens = function() {
        var s = '';

        $('input[name=schLens]').each(function(i) {
		if (this.checked)
		{
			if($(this).val()=='single')
			{
				s = 'products_lens=single';
			}
		}

        });

        return s;
};

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

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

        return s;
};

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

        $('#schTri').each(function(i) {
                if (this.checked) s = 'products_trifocal=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 checkLensWidth = function() {
        var s = $('#schLensWidth').val();

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

var checkTempleLength = function() {
        var s = $('#schTempleLength').val();

        return ('' != s) ? 'schTempleLength=' + 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 checkBorder = function() {
        var s = '';

        $('#schLensBorder').each(function(i) {
                if (this.checked) s = 'lens_border=semi';
        });

        return s;
};

var checkShape = function() {
        var s = $('#schShape').val();

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

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

        if (_t = checkKeywords()) {
                s += (('' == s) ? '' : '&') + _t;
        }
        if (_t = checkGender()) {
                s += (('' == s) ? '' : '&') + _t;
        }
	if (_t = checkLens()) {
                s += (('' == s) ? '' : '&') + _t;
        }
        if (_t = checkCataIds()) {
                s += (('' == s) ? '' : '&') + _t;
        }
        if (_t = checkBifocal()) {
                s += (('' == s) ? '' : '&') + _t;
        }
        if (_t = checkProg()) {
                s += (('' == s) ? '' : '&') + _t;
        }
        if (_t = checkTri()) {
                s += (('' == s) ? '' : '&') + _t;
        }
        if (_t = checkVideo()) {
                s += (('' == s) ? '' : '&') + _t;
        }
        if (_t = checkSize()) {
                s += (('' == s) ? '' : '&') + _t;
        }
        if (_t = checkLensWidth()) {
                s += (('' == s) ? '' : '&') + _t;
        }
        if (_t = checkTempleLength()) {
                s += (('' == s) ? '' : '&') + _t;
        }
        if (_t = checkPrice()) {
                s += (('' == s) ? '' : '&') + _t;
        }
        if (_t = checkSortby()) {
                s += (('' == s) ? '' : '&') + _t;
        }
        if (_t = checkColor()) {
                s += (('' == s) ? '' : '&') + _t;
        }
        if (_t = checkShape()) {
                s += (('' == s) ? '' : '&') + _t;
        }
        if (_t = checkBorder()) {
                s += (('' == s) ? '' : '&') + _t;
        }

        return s;
};

//Woody change search links to rewrite type 20100818
function GetQueryString(name, params)
{
var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
var r = params.match(reg);
if (r!=null) return unescape(r[2]); return null;
}
/************** todo ************/
var getLst = function(s) {

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

	}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) ? '' : '?';

		var url = '';

		switch( GetQueryString('gender_id', param) ){
			case '0':
			case '1':
				if( GetQueryString('gender_id', param) == 0 ){
					url = 'men';
					param = param.replace('gender_id=0','');
				}else{
					url = 'women';
					param = param.replace('gender_id=1','');
				}

				$t = GetQueryString('categories_id', param);
				if($t){
					switch($t){
						case '53':
							url += '-rimless-eyeglasses';
							break;
						case '49':
							url += '-flexlite-eyeglasses';
							break;
						case '48':
							url += '-fashion-eyeglasses';
							break;
						case '54':
							url += '-titanium-eyeglasses';
							break;
						case '55':
							url += '-sport-eyeglasses';
							break;
						case '80':
							url += '-party-eyeglasses';
							break;
						case '56':
							url += '-classic-eyeglasses';
							break;
						case '47':
							url += '-designer-eyeglasses';
							break;
						case '52':
							url += '-plastic-eyeglasses';
							break;
						case '51':
							url += '-metal-eyeglasses';
							break;
						case '71':
							url += '-reading-eyeglasses';
							break;
						case '70':
							url += '-sunglasses';
							break;
						case '72,86':
							url += '-prescription-sunglasses';
							break;
						default:
							url += '-' + $t + '-eyeglasses';
							break;
					}
					param = param.replace('categories_id='+$t,'');
					break;
				}

				$t = GetQueryString('products_bifocal', param);
				if( $t == '1' ){
					url += '-bifocal-eyeglasses';
					param = param.replace('products_bifocal=1','');
					break;
				}

				$t = GetQueryString('products_prog', param);
				if( $t == '1' ){
					url += '-progressive-eyeglasses';
					param = param.replace('products_prog=1','');
					break;
				}

				url += '-eyeglasses';
				break;
			default:
			/*
				$t = GetQueryString('p_price_range_ids', param);
				if( $t ){
					url = 'glasses-price-' + $t;
					param = param.replace('p_price_range_ids=' + $t,'');
					break;
				}
			*/
				$t = GetQueryString('products_bifocal', param);
				if( $t == 1 ){
					url = 'bifocal-eyeglasses';
					param = param.replace('products_bifocal=1','');
					break;
				}
				$t = GetQueryString('products_prog', param);
				if( $t == 1 ){
					url = 'progressive-eyeglasses';
					param = param.replace('products_prog=1','');
					break;
				}
				$t = GetQueryString('lens_border', param);
				if( $t == 'semi' ){
					url = 'semi-rimless-eyeglasses';
					param = param.replace('lens_border=semi','');
					break;
				}
				$t = GetQueryString('categories_id', param);
				if( $t == '70,72' ){
					url = 'sunglasses';
					param = param.replace('categories_id='+$t,'');
					break;
				}
				if( $t ){
					url = 'eyeglasses-c-' + $t;
					param = param.replace('categories_id='+$t,'');
					break;
				}
				url = 'catalog';
				break;
		}

		url += '.html';
		if( param.length > 0 ) {
			url += '?' + param;
			while( url.indexOf('?&') > -1 ){
				url = url.replace('?&','?');
			}
			while( url.indexOf('&&') > -1 ){
				url = url.replace('&&','&');
			}
		}
//alert(url);return;
		window.location.href = url;
		//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();
});
LensCheckbox.click(function() {

	if($(this).val()=='bifocal')
	{
		if (this.checked)
		{
		 $("#schBifocal").attr('checked','checked');}else
		{
		 $("#schBifocal").removeAttr('checked');
		}
	}
	if($(this).val()=='progressive')
	{
		if (this.checked)
		{
		 $("#schProg").attr('checked','checked');}else
		{
		 $("#schProg").removeAttr('checked');
		}
	}
        init.val('0');
        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() {
	init.val('0');
        getLst();
});

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

viewImg.live('click', function() {
	init.val('0');
        var s = 'imgView=' + $(this).attr('alt');
	s += '&pg=' + $('.ppage span:eq(0)').html();
        getLst(s);
});

page.live('click', function() {
	init.val('0');
        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 */
