var xmlHttp
var spantowrite

function gettrailobj(divname){ //get document object by name
    if (document.getElementById)
return document.getElementById(divname)
else if (document.all)
return document.all.divname
}

function posTop(eElement)
{
var nTopPos = eElement.offsetTop; 
var eParElement = eElement.offsetParent; 
while (eParElement != null)
{ 
nTopPos += eParElement.offsetTop; 
eParElement = eParElement.offsetParent; 
}
return nTopPos; 
}

function posLeft(eElement)
{
var nLeftPos = eElement.offsetLeft; 
var eParElement = eElement.offsetParent; 
while (eParElement != null)
{ 
nLeftPos += eParElement.offsetLeft; 
eParElement = eParElement.offsetParent; 
}
return nLeftPos; 
}

function get_obj_width(obj){
    //if (navigator.appName=='Netscape')
      //  return obj.clientWidth;
    //else 
        return obj.offsetWidth;
}

function get_obj_height(obj){
    //if (navigator.appName=='Netscape')
      //  return obj.clientHeight;
    //else 
        return obj.offsetHeight;
}

function get_obj_Clientwidth(obj){
        return obj.clientWidth;
}

function get_obj_Clientheight(obj){
        return obj.clientHeight;
}

function stateChanged() 
{ 
var spanobj=gettrailobj(spantowrite)
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{ 
spanobj.innerHTML=xmlHttp.responseText 
} 
} 

function GetXmlHttpObject()
{ 
var objXMLHttp=null
if (window.XMLHttpRequest)
{
objXMLHttp=new XMLHttpRequest()
}
else if (window.ActiveXObject)
{
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
}
return objXMLHttp
} 
function popupWindow(url) {
  window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,width=100,height=100,screenX=150,screenY=150,top=150,left=150')
}

$(function(){
if(!$.browser.msie || $.browser.version != "6.0"){ 
    $('.rounded')
        .each(function(){
            el = $(this);
            el.html('<div class="rtop"><div class="rtop1"></div><div class="rtop2"></div><div class="rtop3"></div><div class="rtop4"></div></div>' + el.html() + '<div class="rtop"><div class="rtop4"></div><div class="rtop3"></div><div class="rtop2"></div><div class="rtop1"></div></div>');
            
            rnel1 = el.children('.rtop');
            rnel2 = el.children('.rtop').children('div');
            
            rnel2.css('background-color', el.css('background-color'));
            //console.log('-' + el.css('padding-left'));
            
            rnel1.css('margin-left', '-' + el.css('padding-left'));
            rnel1.css('margin-right', '-' + el.css('padding-right'));
        });
}

    $('a.categoriesTextActive').each(function() {
        $(this).attr('href', $(this).attr('href').replace(/\??resultsperpg\=viewall\&?/, ''));
    });
});

function clickLivePersonButton() {
    if($('#filterDialog').length > 0) {
        $('#filterDialog').dialog('close');
    }

    lpButtonCTTUrl = 'https://server.iad.liveperson.net/hc/6375852/?cmd=file&file=visitorWantsToChat&site=6375852&imageUrl=https://server.iad.liveperson.net/hcp/Gallery/ChatButton-Gallery/English/Retail/3a&referrer='+escape(document.location);
    lpButtonCTTUrl = (typeof(lpAppendVisitorCookies) != 'undefined' ? lpAppendVisitorCookies(lpButtonCTTUrl) : lpButtonCTTUrl);
    lpButtonCTTUrl = ((typeof(lpMTag)!='undefined' && typeof(lpMTag.addFirstPartyCookies)!='undefined')?lpMTag.addFirstPartyCookies(lpButtonCTTUrl):lpButtonCTTUrl);
    window.open(lpButtonCTTUrl,'chat6375852','width=475,height=410,resizable=yes');

    return false;
}

function selectElementText(el) {
    el.focus();
    el.select();
}

/* product comparison */
function select_to_compare(product_id) {
    if($('#compare_product_' + product_id)[0].checked) {
        $.post('ajax_handler.php?page=compare_products',
            {product_id: product_id, action: 'add'},
            function() {
                $('#select_to_compare_' + product_id).hide();
                $('#compare_selections_' + product_id).show();
            }
        );
    } else {
        $.post('ajax_handler.php?page=compare_products',
            {product_id: product_id, action: 'delete'},
            function() {
                $('#select_to_compare_' + product_id).show();
                $('#compare_selections_' + product_id).hide();
            }
        );
    }
}

function remove_compared_product(product_id) {
    $('.product_info_' + product_id).remove();
    $('.compare_data_table').width(175 * $('.product_info_header').length);
    
    if($('.compare_data_table').width() < $('.data_overflow').width()) {
        $('.data_overflow').width($('.compare_data_table').width());
        $('.header_top_compare_table').width($('.compare_data_table').width()+75);
    }

    var left = -parseInt($('.compare_data_table').css('left'));
    if(left > 0) {
        $('.compare_data_table').animate(
            {left: "+=" + left},
            600
        );
    }
    
    var prev_button = $('#compare_prev_button');
    var max = parseInt(prev_button.attr('max'));
    if(max > 0) {
        prev_button.attr('max', max - 1);
    }
    prev_button.attr('current', 0);
    init_compare_buttons()
    
    if($('.product_info').length == 0) {
        $('#compare_help_text').show();
        $('.info_compare_table').hide();
        $('.share').hide();
    }

    $.post('ajax_handler.php?page=compare_products',
        {product_id: product_id, action: 'delete'}
    );
}

function init_compare_buttons() {
    var prev_button = $('#compare_prev_button');
    var next_button = $('#compare_next_button');
    
    var current = parseInt(prev_button.attr('current'));
    var max = parseInt(prev_button.attr('max'));
    
    if(current < max) {
        next_button.css('visibility', 'visible');
    } else {
        next_button.css('visibility', 'hidden');
    }
    if(current > 0) {
        prev_button.css('visibility', 'visible');
    } else {
        prev_button.css('visibility', 'hidden');
    }
}

function compare_next() {
    var prev_button = $('#compare_prev_button');
    
    var current = parseInt(prev_button.attr('current'));
    var max = parseInt(prev_button.attr('max'));
    var column_width = $('.product_info').width();
    
    if(current < max) {
        prev_button.attr('current', current + 1);
        
        $('.compare_data_table').animate(
            {left: "-=" + (column_width + 3)},
            600
        );
    }
    
    init_compare_buttons();
}
function compare_prev() {
    var prev_button = $('#compare_prev_button');
    
    var current = parseInt(prev_button.attr('current'));
    var column_width = $('.product_info').width();
    
    if(current > 0) {
        prev_button.attr('current', current - 1);
        
        $('.compare_data_table').animate(
            {left: "+=" + (column_width + 3)},
            600
        );
    }
    
    init_compare_buttons();
}
function init_compare_table() {
    var total_records = $('.product_info_header').length;
    
    for(var i = 5; i<=total_records; i++) {
        $('.product_info_row_'+(i-1)).hide();
    }
}

$().ready(function() {
    var compareTableHeader = $('.header_top_compare_table');
    if(compareTableHeader.length > 0) {
        var compareTableHeaderOffset = $(compareTableHeader).offset();
        
        $(window).scroll(function() {
            if($(this).scrollTop() > compareTableHeaderOffset.top){
                compareTableHeader.css('position', '');
                compareTableHeader.addClass('freeze');
                compareTableHeader.css('left', compareTableHeaderOffset.left);
            }else{
                compareTableHeader.removeClass('freeze');
                compareTableHeader.css('left', '0');
                compareTableHeader.css('position', 'relative');
            }
        }).scroll();
        
        remove_compared_product(0);
    }
});

// newsletter input box
$(function() {
    var intro_text = 'Enter email for exclusive offers!';
    if ($('input.newsletter_email').val() == ""){
        $('input.newsletter_email').val(intro_text).css('color', '#ababab');
    }
    $('input.newsletter_email').click(function(){
        if($(this).val() == intro_text) $(this).css('color', '').val('');
    }).blur(function(){
        if($(this).val() == '') $(this).val(intro_text).css('color', '#ababab');
    })
});

// track js events
$(function() {
    $('.create_new_design').click(function() {
        trackJSEvent('create-new-design');
    });
    
    $('.add_to_wishlist_button').click(function() {
        trackJSEvent('add-to-wishlist');
    });
    
    $('#_lpChatBtn').click(function() {
        trackJSEvent('livechat-header');
    });
    
    $('.lpInviteChatHrefAccept').live('click', function() {
        trackJSEvent('livechat-invitation');
    });
});

