var commentInfoTplVar = '';
var countCompare = 0;
var listCompare = new Array();

function buildUrl(_prm){
	var _url = '/?';
	for (n in _params_page){
		_url = _url + n + '=' + _params_page[n] + '&';
	}
	for (n in _prm){
		_url = _url + n + '=' + _prm[n] + '&';
	}
	return _url;
}

function countListCompare(){
	countCompare = 0;

	$(".compareElement").each(function (){
		countCompare = parseInt(countCompare) + 1;
	});

	return countCompare;
}

function displayCompare(){
	countListCompare();
	if (parseInt(countCompare) > 0){
		$("#div-compare-list").css('display', 'block');
		$(".div-compare-sublist-main").css('display', 'block');
	}else{
		$("#div-compare-list").css('display', 'none');
		$(".div-compare-sublist-main").css('display', 'none');
	}
}

function putCompare(id){
	var htmlTd = false;
	$("#compareListJs").append('<td class="compareElement" id="compareElement_' + id + '"><div style="margin: 0px 5px 0px 5px;"><img src="/images/ajaxLoader.gif" style="width: 38px; height: 38px;" /></div></td>');

	countListCompare();
	mkBindCloseDiv();

	if (parseInt(countCompare) > 1){
		$(".makeCompare").css('display', 'block');
	}else{
		$(".makeCompare").css('display', 'none');
	}

	var textId = "_typeInfo_" + id;
	var compareType = '';
	$(".itmDivType").each(function (){
		if ($(this).attr("id") == textId){
			compareType = $(this).html();
		}
	});

	displayCompare();
	$.ajax({
		url: buildUrl(),
		data: {'id[]': id, 'compare': 'compare', 'compareType': compareType, 'display_ajax': 'compare'},
		type: "POST",
		success: function(data){
			var textId = "compareElement_" + id;
			$(".compareElement").each(function (){
				if ($(this).attr("id") == textId){
					$(this).html(data);
					mkBindCloseDiv();
				}
			});
		}
	});
}

function mkBindCloseDiv(){
	$(".compareElement").mouseover(function (){
		$(this).find(".compareCloseDiv").each(function (){
			$(this).css('display', 'block');
		});
	});

	$(".compareElement").mouseout(function (){
		$(this).find(".compareCloseDiv").each(function (){
			$(this).css('display', 'none');
		});
	});
}

function addCompare(id){
	var textId = "itemCompare_" + id;
	var remove = true;
	$(".itemCompareCheck").each(function (){
		if ($(this).attr("id") == textId && $(this).is(':checked')){
			remove = false;
		}
	});

	if (remove){
		removeCompare(id);
	}else{
		countListCompare();
		putCompare(id);
	}
	displayCompare();
}

function removeCompare(id){
	$(".compareElement").each(function (){
		var textId = "compareElement_" + id;
		if ($(this).attr("id") == textId){
			$(this).remove();
		}
	});

	$(".itemCompareCheck").each(function (){
		var textId = "itemCompare_" + id;
		if ($(this).attr("id") == textId){
			$(this).attr("checked", false);
		}
	});

	countListCompare();

	if (parseInt(countCompare) > 0){
		$(".div-compare-sublist-main").css('display', 'block');
	}else{
		$(".div-compare-sublist-main").css('display', 'none');
	}

	if (parseInt(countCompare) < 2){
		$(".makeCompare").css('display', 'none');
	}else{
		$(".makeCompare").css('display', 'block');
	}

	$.ajax({
		url: buildUrl(),
		data: {'id[]': id, 'remove_compare': 'remove_compare', 'display_ajax': 'compare'},
		type: "POST",
		success: function(data){
		}
	});
}

function commonIdDisplayMehr(){
    $(".commonIdDisplayMehr").unbind('click');
    $(".commonIdDisplayMehr").bind('click', function (){
    	$("." + $(this).attr('id')).css('height', 'auto');
    	$("." + $(this).attr('id')).css('display', 'block');
    	$(this).css('display', 'none');
    	return false;
    });
}

function commonDisplayMehr(){
    $(".commonDisplayMehr").unbind('click');
    $(".commonDisplayMehr").bind('click', function (){
    	$("." + $(this).attr('href')).css('height', 'auto');
    	$("." + $(this).attr('href')).css('display', 'block');
    	$(this).css('display', 'none');
    	return false;
    });
}

function commonMehr(){
    $(".commonMehr").unbind('click');
    $(".commonMehr").bind('click', function (){
    	$("#" + $(this).attr('href')).css('overflow', 'visible');
    	$("#" + $(this).attr('href')).css('height', 'auto');
    	$("#" + $(this).attr('href')).css('margin-bottom', '10px');
    	$(this).css('display', 'none');
    	return false;
    });
}

var afterBmodalFunction = function (){
    var resBpoPup = $("#divPopUp").bPopup({
        'position': ['auto', 'auto'],
        'modalColor': '#ccc',
        'follow': [false, false]
    });

    $(".popupClose div").bind('click', function (){
    	resBpoPup.close();
    });

    commonMehr();
    commonDisplayMehr();
}

function modalPopup(source, typeModal, callback, _params){
	if (typeof _params == 'undefined'){
		_params = {};
	}

	$("#divPopUp").css('border', '0px');
	$("#divPopUp").html('<div class="loadingParentInfo"><div class="loadingInfo">&nbsp;</div></div>');
	$("#divPopUp").css('margin-left', 0);
	$("#divPopUp").css('margin-top', 0);
    $("#divPopUp").bPopup({
        'position': ['auto', 'auto'],
        'modalColor': '#ccc',
        'follow': [false, false]
    });

	if (typeof typeModal == 'undefined' || typeModal == 'url'){
		$.ajax({
			url: source,
			type: "GET",
			success: function(data){
				$("#divPopUp").css('border', 'solid 1px #9BA3AA');
				$("#divPopUp").html(data);

				if (typeof callback != 'undefined'){
					callback(_params, data, afterBmodalFunction);
				}else{
					afterBmodalFunction();
				}
			}
		});
	}else{
		$("#divPopUp").css('border', 'solid 1px #9BA3AA');
		$("#divPopUp").html(source);

		if (typeof callback != 'undefined'){
			callback(_params, source, afterBmodalFunction);
		}else{
			afterBmodalFunction();
		}
	}
}

function mkBindInfo(clickId){
	if (typeof clickId == 'undefined'){
		clickId = ".linkInfo";
	}

	divId = '';
	$(clickId).bind('click', function (){
		modalPopup($(this).attr("href"));
	    return false;
	});
}

function divByValueInput(obj){
	var divId = $(obj).val();
	if ($(obj).is(':checked')){
		$(".divItem12:not(." + divId + ")").css('display', 'none');
	}else{
		$(".divItem12").css('display', 'block');
		$(".divByValueInput").each(function (){
			if ($(this).is(':checked')){
				var divId = $(this).val();
				$(".divItem12:not(." + divId + ")").css('display', 'none');
			}
		});
	}
}

var sendAjx = true;

function mkFindList(word){
	if (sendAjx){
		sendAjx = false;
		$("#listMarksAjaxLdr").addClass('ajaxLoaderBall');
		$.ajax({
			url: buildUrl(),
			data: {'valMark': word, 'mkListMark': true},
			type: "POST",
			success: function(data){
				$("#listMarks").html(data);
				sendAjx = true;
				$("#listMarksAjaxLdr").removeClass('ajaxLoaderBall');
			}
		});
	}
}

var wordFindMark = '';

var showCounter = function (){
    var ifr = document.createElement('iframe');
    ifr.src = window.location.href.replace(/#.*$/, '') + (window.location.href.indexOf('?') != -1 ? '&' : '?') + 'clickout_html=' + Math.random();
    with(ifr.style) {
        border = 0;
        width = '1px';
        height = '1px';
        position = 'absolute';
        top = '-10px';
        left = '-10px';
    }
    document.body.appendChild(ifr);
}

$(document).ready(function (){
	$("#divPopUp").html('');
	$("#divPopUp").css('position', 'absolute');
	$("#divPopUp").css('margin-left', -1000);
	$("#divPopUp").css('margin-top', -1000);
	$("#divPopUp").css('display', 'block');

	mkBindInfo();

	$(".divByValueInput").bind('click', function (){
		divByValueInput(this);
	});

	$(".markAutocomplete").keyup(function (obj){
		setTimeout(function (){
			wordFindMark = $(".markAutocomplete").val();
			mkFindList(wordFindMark);
		}, 1500);
	});

	$(".divItem").mouseover(function (){
		$(this).addClass('activeItemList');
	});

	$(".divItem").mouseout(function (){
		$(this).removeClass('activeItemList');
	});

	mkBindCloseDiv();

	if ($("#tplCommentInfo").html() === null){
		commentInfoTplVar = '';
	}else{
		commentInfoTplVar = $("#tplCommentInfo").html();
		$("#tplCommentInfo").html('');
	}

	if (searchWordRemove != ''){
		$("#wordsearch").bind('click', function(){
			if ($(this).val() == searchWordRemove){
				$(this).val('');
			}
		});
	}
});

function mkVote(){
	var chooseVote = $("#divFormVote input:checked").val();
	var loadLink = buildUrl({'chooseVote': chooseVote, 'action': 'vote'});

	$("#divFormVote").load(loadLink, function (){

	});
}

function showListCompare(){
	modalPopup('?action=comparelist');
}

function showDebug(text){
	$("#debug").append(text + '<br/>');
}

function lineBgPrice(){
	if (addDivLine){
		addDivLine = false;
	    $("#priceSlider").append('<div id="minSlidePrice" class="minSlidePrice"></div>');
	}

	var widthMath = parseFloat(maxSlider) - parseFloat(minSlider);
	var widthSlider = $("#priceSlider").width();

	var widthPerDigit = parseFloat(widthSlider)/parseFloat(widthMath);

	var halfWidth = parseInt($(".ui-slider-handle-second").width()/2);

	var leftMid = parseFloat(widthPerDigit)*parseFloat($("#min_price").val()) - halfWidth;
	var widthMid = (parseFloat($("#max_price").val()) - parseFloat($("#min_price").val())) * widthPerDigit + halfWidth;

    $("#minSlidePrice").css('left', leftMid + 'px')
    $("#minSlidePrice").css('top', '-1px');
    $("#minSlidePrice").css('width', widthMid + 'px');
    $("#minSlidePrice").css('height', parseFloat($("#priceSlider").height()) + 1 + 'px');
    $("#minSlidePrice").css('display', 'block');
}

var assignInfoProductComment = function(_params, data, _fnc){
	$("#formCommentProduct").append('<input type="hidden" name="id" value="' + _params.sendId + '" />');
	$("#formCommentProduct").append('<input type="hidden" name="typeProduct" value="' + _params.itemType + '" />');
	$("#formCommentProduct").append('<input type="hidden" name="feedType" value="' + _params.feedType + '" />');
	$("#formCommentProduct").append('<input type="hidden" name="clickType" value="' + _params.clickType + '" />');

	$("#imgSrcComment").html('<img src="' + $("#_imgInfo_" + _params.id).attr('src') + '" />');
	$("#titleProductComment").html($("#_title_" + _params.id).html());
	$("#descProductComment").html($("#_descInfo_" + _params.id).html());
	$(".checksCommentVote").attr('checked', false);
	$(".inputCommentInit").val('');

	$("#commentDecription .zumShop a").attr('href', $("#zumShopLnk_" + _params.id).attr("href"));

	_fnc();
}

function showComment(id, type, _click_type){
	if (commentInfoTplVar != ''){
		var sendId = $("#_id_" + id).html();
		_params = {'feedType': _feed_type, 'clickType': _click_type, 'id': id, 'itemType': type, 'sendId': sendId};

		modalPopup(commentInfoTplVar, 'text', assignInfoProductComment, _params);
	}

    return false;
}

function CommentSubmit(){
	$("#formDivCommentProduct").fadeOut(function (){$("#thanksComment").fadeIn()});
}
