function show_cyo_earring_diamond_popup(index) {
    $("#earring_diamond_0").hide();
    $("#earring_diamond_1").hide();
    $("#earring_diamond_" + index).show();
    show_prduct_images_popup('sample_image_main');
}

function show_prduct_images_popup(div_id) {
    try {
        var src = product_images[current_caption]['lg'];
        $("#product_image_large").attr('src', src);
        $("#image_icon").find('a').removeClass('selected');
        $("#sample_image_icon_" + current_caption).addClass('selected');
    } catch(e) {}

    var objDiv = document.getElementById(div_id);
    var diffHeight=(document.documentElement.scrollHeight/2)-document.documentElement.scrollTop;
    objDiv.style.display = "inline";
    var obj_width = getWinWidth()/2 - objDiv.offsetWidth/2;
    if (obj_width < 0)
        obj_width = 0;
    objDiv.style.left= obj_width +'px';
    $(objDiv).show();
    resizeOverlay();
	return false;
}

function change_product_middle_image(caption, obj) {
    var src = product_images[caption]['md'];
    var alt = product_images[caption]['alt'].replace(', large', ',');
    $("#product_image_middle").attr('src', src);
    $("#product_image_middle").attr('alt', alt);

    if (obj != undefined) {
        current_caption = caption;
        $("#product_thumb_images").find('li').removeClass('selected');
        $(obj).parent().addClass('selected');
    }
}

function change_product_thumb_image(caption) {
    var src = product_images[caption]['tn'];
    $("#product_thumb_images").find('li').eq(0).find('img').attr('src', src);
    $("#image_icon").find('img').eq(0).attr('src', src);
}

function change_product_large_image(caption, obj) {
    var src = product_images[caption]['lg'];
    var alt = product_images[caption]['alt'];
    $("#product_image_large").attr('src', src);
    $("#product_image_large").attr('alt', alt);
    $("#image_icon").find('a').removeClass('selected');
    $(obj).addClass('selected');
}

function change_setting_shape(obj) {
    try {
        var shape = $(obj).val();
        product_images['top_lg']['tn'] = product_shape_images[shape]['top_lg']['tn'];
        product_images['top_lg']['md'] = product_shape_images[shape]['top_lg']['md'];
        product_images['top_lg']['lg'] = product_shape_images[shape]['top_lg']['lg'];
        product_images['side_lg']['tn'] = product_shape_images[shape]['side_lg']['tn'];
        product_images['side_lg']['md'] = product_shape_images[shape]['side_lg']['md'];
        product_images['side_lg']['lg'] = product_shape_images[shape]['side_lg']['lg'];
    }
    catch(e) {}

    try {
        // for matched sets only
        product_images['ring_top_lg']['tn'] = product_shape_images['ring_' + shape]['top_lg']['tn'];
        product_images['ring_top_lg']['md'] = product_shape_images['ring_' + shape]['top_lg']['md'];
        product_images['ring_top_lg']['lg'] = product_shape_images['ring_' + shape]['top_lg']['lg'];
        src = product_images['ring_top_lg']['tn'];
        $("#product_thumb_images").find('li').eq(1).find('img').attr('src', src);
        $("#image_icon").find('img').eq(1).attr('src', src);
    }
    catch(e) {}

    change_product_middle_image('top_lg');
    change_product_thumb_image('top_lg');
    $("#product_thumb_images").find('li').removeClass('selected');
    $("#product_thumb_images").find('li').eq(0).addClass('selected');
    current_caption = 'top_lg';
    try{
        cyo_shape = $('#shape-select :selected').text();
        $.cookie('cyo_shape', cyo_shape, {path: '/'});
    }
    catch(e) {}
}

function ShowPopUp()
{
    if(arguments.length == 2){
        //show image
        $("div.picConImg div").each(function(){ $(this).attr('style', 'display:none'); });
        var div = $("div.picConImg img").not($("div p a img")).eq(arguments[1]).parents('div')[0];
        div.style.display = 'block';
        if(div.id.indexOf('zoom') != -1){
            div = div.parentNode;
            div.style.display='block';
        } else{
            if($('div#zoom_in').length > 0){
                $('div#zoom_in').attr('style', 'display:block;');
            }else{
                $('div#zoom_out').attr('style', 'display:block;');
            }
        }


        //switch icons
        icons = $('span#image_icon img');
        if(icons.length > 0){
            index = $.inArray(div, $("div.picConImg > div"));
            icons.each(function(){ this.src = set_inactive(this.src) });
            icons[index].src = set_active(icons[index].src);
        }
    }

	try{
		var objDiv = document.getElementById(arguments[0]);
		var diffHeight=(document.documentElement.scrollHeight/2)-document.documentElement.scrollTop;
		objDiv.style.display = "inline";
		var obj_width = getWinWidth()/2 - objDiv.offsetWidth/2;
		if (obj_width < 0)
		    obj_width = 0;
		objDiv.style.left= obj_width +'px';
		$(objDiv).show();
		resizeOverlay();
	}
	catch(e){}
	return false;
}

function resizeOverlay(){
	var overlay = $('#overlay');
	var dw = Math.max(document.documentElement.scrollWidth,document.documentElement.clientWidth);
	var dh = Math.max(document.documentElement.scrollHeight,document.documentElement.clientHeight);
	overlay.width(dw);
	overlay.height(dh);
	overlay.show();	
}

function getWinWidth()
{
    if (window.innerWidth)
    {
    	w = window.innerWidth;
    	if (document.body.scrollHeight && document.body.scrollHeight >= getWinHeight())
    		w-=15;
    	return w;
    }
    else if (document.documentElement && document.documentElement.clientWidth) 
  		return document.documentElement.clientWidth;
    else if (document.body && document.body.clientWidth) 
  		return document.body.clientWidth;
  	else if (document.body && document.body.parentNode && document.body.parentNode.clientWidth) 
 		return document.body.parentNode.clientWidth;
}

function getWinHeight()
{
    if (window.innerHeight) return window.innerHeight;
  	else if (document.documentElement && document.documentElement.clientHeight) 
  		return document.documentElement.clientHeight;
  	else if (document.body && document.body.clientHeight) 
  		return document.body.clientHeight;
  	else if (document.body && document.body.parentNode && document.body.parentNode.clientHeight) 
  		return document.body.parentNode.clientHeight;
}
function ShowOverlay()
{
	$('#overlay').show(); 
}

function HideOverlay()
{
	$('#overlay').hide();
}

function CloseProductImagePopUp(DivName){
	$('#' + DivName).hide();
	$('#overlay').hide();
}

function change_setting(){
	return confirm("Changing your setting will delete your selection. Are you sure you want to do this?");
}

function change_diamonds(){
	return confirm("Changing your diamonds will delete your selection. Are you sure you want to do this?");
}

function change_diamond(){
	return confirm("Changing your diamond will delete your selection. Are you sure you want to do this?");
}

function show_similar(){
	// This is used for diamond similar tab and setting related tab.
	if($('#similar_tab_detail').is('.active')) {
	$('#similar_tab_detail').removeClass('active');
	$('#similar_tab_similar').addClass('active');
	$('#similar_div_detail').hide();
	$('#similar_div_similar').show();
	}
}

function show_detail(){
	// This is used for diamond similar tab and setting related tab.
	if($('#similar_tab_similar').is('.active')){
		$('#similar_tab_detail').addClass('active');
		$('#similar_tab_similar').removeClass('active');
		$('#similar_div_detail').show();
		$('#similar_div_similar').hide();
    }
}

function get_print_url(){
	var cur_url = location.href;
	var pattern = /[^#]+/
	var res = cur_url.match(pattern);
	if (res) {
		cur_url = res[0]
	}
	if (cur_url.indexOf('?') >= 0) {
		return cur_url + '&print=True';
	} else {
		return cur_url + '?print=True';
	}
}

function changepic(img_id, img, dict) {
	/* replace img_id div with img, and traverse dict replace dict key div with dict value src.*/
	$("#" + img_id).attr("src", img);
	try{
		for (var id in dict) {
			$("#" + id).children("img").attr("src", dict[id]);
		}
	}
	catch(e){}
}

function set_active(src) {
    if (is_active(src) == -1) {
        src = src.replace(/.jpg/, "_active.jpg");
    }
    return src
}

function set_inactive(src) {
    if (is_active(src) != -1) {
        src = src.replace(/_active/, "");
    }
    return src
}

function is_active(src) {
    return src.search(/_active.jpg$/)
}

function get_img_id(id) {
    return id + "_img";
}

