﻿var _gigabyte_cid;
var _gigabyte_model_json;
var _gigabyte_model_cnt;

function read_comparison_model() {
	if ($.cookie('comparison_pids_' + _gigabyte_cid) != null) {
		if ($.cookie('comparison_pids_' + _gigabyte_cid) == "") {
			$.cookie('comparison_pids_' + _gigabyte_cid, '', { expires: -1 });
			$.cookie('comparison_pnames_' + _gigabyte_cid, '', { expires: -1 });
			_gigabyte_model_json = { "count": 0 };
			_gigabyte_model_cnt = 0;
		}
		else {
			var arr_pid = $.cookie('comparison_pids_' + _gigabyte_cid).split(',');
			var arr_pname = $.cookie('comparison_pnames_' + _gigabyte_cid).split(',');

			_gigabyte_model_json = { "count": arr_pid.length, "pid": arr_pid, "pname": arr_pname };
			_gigabyte_model_cnt = arr_pid.length;
		}
	}
	else {
	    _gigabyte_model_json = { "count": 0 };
	    _gigabyte_model_cnt = 0;
	}
}

function write_comparison_model(pid, name) {
	if (isNaN(pid) == false) {
		var p = parseInt(pid);
		if (p == -1) {
			$.cookie('comparison_pids_' + _gigabyte_cid, '', { path: "/", expires: -1 });
			$.cookie('comparison_pnames_' + _gigabyte_cid, '', { path: "/", expires: -1 });
		} else {
			if (p != 0) {
			    if ($.cookie('comparison_pids_' + _gigabyte_cid) == null) {
				    $.cookie('comparison_pids_' + _gigabyte_cid, pid, { path: "/" });
				    $.cookie('comparison_pnames_' + _gigabyte_cid, name, { path: "/" });
				} else {
					var i_key = -1;
					var arr_pid = $.cookie('comparison_pids_' + _gigabyte_cid).split(',');
					var arr_pname = $.cookie('comparison_pnames_' + _gigabyte_cid).split(',');
					for (var i = 0; i < arr_pid.length; i++) {
						if (arr_pid[i] != "" && isNaN(arr_pid[i]) == false) {
							var k = parseInt(arr_pid[i]);
							if (p == k) {
								i_key = i;
								break;
							}
						}
					}
					var arr_tmp_id = [];
					var arr_tmp_name = [];
					if (i_key == -1) {
						for (var i = 0; i < arr_pid.length; i++) {
							if (i != i_key && arr_pid[i] != "") {
								arr_tmp_id.push(arr_pid[i]);
								arr_tmp_name.push(arr_pname[i]);
							}
						}
						if (arr_pid.length < 10) {
							arr_tmp_id.push(pid);
							arr_tmp_name.push(name);
						}
						else {
						    alert(_gigabyte_maxselect);
						}
					}
					else {
						for (var i = 0; i < arr_pid.length; i++) {
							if (i != i_key && arr_pid[i] != "") {
								arr_tmp_id.push(arr_pid[i]);
								arr_tmp_name.push(arr_pname[i]);
							}
						}
					}
					$.cookie('comparison_pids_' + _gigabyte_cid, arr_tmp_id.join(','), { path: "/" });
					$.cookie('comparison_pnames_' + _gigabyte_cid, arr_tmp_name.join(','), { path: "/" });
				}
			}
		}
	}
}

function review_models(t) {
    read_comparison_model(); 
	if (_gigabyte_model_cnt > 0) {
		var arr_pid = _gigabyte_model_json.pid;
		var arr_pname = _gigabyte_model_json.pname;
		$('#tb_chked_models').empty();
		for (var i = 0; i < arr_pid.length; i++) {
			if (i > 0) $('#tb_chked_models').append('<tr><td style="height:1px; background:#C9C9C9;" colspan="2"> </td></tr>');
			$('#tb_chked_models').append('<tr id="tr_chked_model_' + arr_pid[i] + '" onMouseover="$(\'#img_delmodel_' + arr_pid[i] + '\').show();" onMouseout="$(\'#img_delmodel_' + arr_pid[i] + '\').hide();"></tr>');
			$('#tr_chked_model_' + arr_pid[i]).append('<td width="20"><img src="/images/icon_del.png" id="img_delmodel_' + arr_pid[i] + '" style="cursor:pointer;display:none;" onclick="chk_chg_cookie(\'' + t + '\', ' + arr_pid[i] + ',\'' + arr_pname[i] + '\');" /></td>');
			$('#tr_chked_model_' + arr_pid[i]).append('<td style="text-align:left;"><a href="/products/product-page.aspx?pid=' + arr_pid[i] + '" target="_blank">' + arr_pname[i] + '</a></td>');
		}
	}
}

function compare_title(pid) {
	if ($('#spn_search_text_title').length > 0) {
		if (_gigabyte_model_cnt == 0) {
			$('#td_form_compare').css({ 'background-image': 'url(images/cart-none.gif)' });
			$('#lbl_compare').show();
			$('#lbl_btn_compare').hide();
			$('#chk_clear_all, #spn_top_clearall').hide();
			$('#spn_top_clearall').html($('#chk_clear_all').html());
		}
		else {
			arr_pid = _gigabyte_model_json.pid;
			arr_pname = _gigabyte_model_json.pname;
			$('#td_form_compare').css({ 'background-image': 'url(images/cart-selected0_move.gif)' });
			$('#div_productname_' + pid).css({ 'height': '25px' });
			timerID = setTimeout(function() {
				selected_gif(pid);
			}, 300);
			$('#lbl_compare').hide();
			$('#lbl_btn_compare').show();
			$('#lbl_model_count').text(_gigabyte_model_cnt);
			$('#hy_btn_compare').addClass('comparetitle');
			$('#img_clear_model').attr({ src: '/images/checkbox_none.gif' });
			$('#chk_clear_all, #spn_top_clearall').show();
			$('#spn_top_clearall').html($('#chk_clear_all').html());
		}
	}
}

function checkmodellist() {
	if ($('#spn_search_text_title').length > 0) {
		var arr_pid = [];
		if (_gigabyte_model_cnt > 0) { var arr_pid = _gigabyte_model_json.pid; }
		var bln_chked;
		$('[id^=hid_pid_]').each(function() {
			var k = $(this).val();
			bln_chked = false;
			if (arr_pid.length > 0) {
				for (var i = 0; i < arr_pid.length; i++) {
					if (arr_pid[i] == k) {
						bln_chked = true;
						break;
					}
				}
			}
			if (bln_chked) {
				$('#chk_' + k).attr({ src: '/images/checkbox_checked.gif' });
			}
			else {
				$('#chk_' + k).attr({ src: '/images/checkbox_none.gif' });
			}
		});
	}
}

function list_icon_ctrl() {
    if (_gigabyte_model_cnt == 0) {
        $('#compareicon').hide();
        $('[id^=com_chk_]').attr('checked', false);
    } else {
        $('#compareicon').show();
        $('#lbl_model_count').text(_gigabyte_model_cnt);
        $('[id^=com_chk_]').attr('checked', false);
        for (var i = 0; i < _gigabyte_model_json.pid.length; i++) {
            if ($('#com_chk_' + _gigabyte_model_json.pid[i]).length > 0) {
                $('#com_chk_' + _gigabyte_model_json.pid[i]).attr('checked', true);
            }
        }
    }
}

function productpage() {
    if (_gigabyte_model_cnt == 0) {
        $('#compa').show();
        $('#pnl_comp').hide();
        $('[id^=com_chk_]').attr('checked', false);
    } else {
        $('#lbl_model_count').text(_gigabyte_model_cnt);
        $('#compa').hide();
        $('#pnl_comp').show();
        $('[id^=com_chk_]').attr('checked', false);
        for (var i = 0; i < _gigabyte_model_json.pid.length; i++) {
            if ($('#com_chk_' + _gigabyte_model_json.pid[i]).length > 0) {
                $('#com_chk_' + _gigabyte_model_json.pid[i]).attr('checked', true);
            }
        }
    }
}

function init_comparison(t, cid, spn_name) {
    layer_modelarea(t, cid, spn_name);    
    if (t == "1") {
        compare_title(0);
    } else if (t == "2") {
        list_icon_ctrl();
    } else if (t == "3") {
        $('#tb_compare').css('width', '30px').css('display', 'inline').css('position', 'absolute');
        productpage();
    }
}

function chk_chg_cookie(t, pid, name) {
    write_comparison_model(pid, name);
    review_models(t);
    if (t == "1") {
        compare_title(pid);
        checkmodellist();
    } else if (t == "2") {
        list_icon_ctrl();
    } else if (t == "3") {
        productpage();
    }
}

function layer_modelarea(t, cid, spn_name) {
	_gigabyte_cid = cid;
	$('#' + spn_name).append('<table id="tb_compare" cellpadding="0" cellspacing="0"></table>');
	$('#tb_compare').append('<tr><td>(<span id="lbl_model_count" style="color:Red; text-decoration:underline;"></span>)</td></tr>');
	$('#tb_compare').append('<tr id="tr_compare_arrowup" style="display:none;"><td><img src="/images/arrowup.gif" /></td></tr>');
	$('#tb_compare').append('<tr id="tr_compare_main" style="display:none;"></tr>');
	$('#tr_compare_main').append('<td id="td_compare_main"></td>');
	$('#td_compare_main').append('<div id="pan_compare_main" style="margin:-5px 0px 0px -112px; width:240px; background:url(/images/shadowNEW.png) no-repeat bottom right; padding:0px 9px 8px 0px; position:absolute; z-index:9999;"></div>');
	$('#pan_compare_main').append('<div id="pan_chked_models" style="background:#FFFFFF url(/images/blue_bg.gif) repeat-x; padding:2px 2px 2px 2px; border:solid 6px #5A91E2;"></div>');
	$('#pan_chked_models').append('<table id="tb_chked_models" style="margin:3px 0px 5px 5px;line-height:200%;" class="modelname" width="215" cellpadding="0" cellspacing="0"></table>');

	$('#tb_compare').hover(function() {
		$('#tr_compare_arrowup, #tr_compare_main').show();
	},
	function() {
		$('#tr_compare_arrowup, #tr_compare_main').hide();
	});
	review_models(t);
}

function comparisonNow() {//結果頁
	if (_gigabyte_model_cnt > 0) {
		window.open('/products/comparison/list.aspx?ck=' + _gigabyte_cid + '&pids=' + _gigabyte_model_json.pid.join(','), '', 'width=1000,height=600,top=300,left=300,resizable=1,scrollbars=1,location=1');
		return false;
	}
}
