$(document).ready(init);

// -------------------------------------------------------------------------
function cart_get_size(parent_href, productid)
{
    $("#loading_container").css("height",getPageSize()+'px');
    $("#loading_container").show();
    //$("#loading_container").fadeIn("fast");
    $("#sizes_popup").load("/cart/get_size/"+productid, '', 
        function(){
            $("#sizes_popup").hide();
            $("#sizes_popup").show();
            $("#sizes_popup").css('top', '30%');
            $("#sizes_popup").css('left', '47%');
        });
return false;
}

function cart_close_sizes(element)
{
    //$("#loading_container").fadeOut("fast");
    $("#loading_container").hide();
    element.parentNode.style.display = 'none';
}

function cart_change_size(productid)
{
	$("#sizes_popup").hide();
    newsize = $("#sizes_select").get(0).selectedIndex;
    $("#cs_main_content").load("/cart/change_size/"+productid+'/'+newsize, '', 
        function(){
            //$("#loading_container").fadeOut("fast");
            $("#loading_container").hide();
        });
return false;
}

function add_to_wishlist_catalogue(productid)
{
    $("#wishlist_head").highlightFade("#c69b3c");
    $("#wishlist").load("/wishlist/add/"+productid, '',
        function(){
            //$("#wishlist_head").highlightFade();
        });
}
function remove_from_wishlist(productid)
{
    $("#wishlist_head").highlightFade("#c69b3c");
    $("#wishlist").load("/wishlist/remove/"+productid, '',
        function(){
            //$("#wishlist_head").highlightFade();
        });
}

function check_register_form()
{
    //$("#register_form").
    
    if (requiered_field(document.forms["registerform"].title) &&
        requiered_field(document.forms["registerform"].name) &&
        requiered_field(document.forms["registerform"].last_name) &&
        requiered_field(document.forms["registerform"].phone) &&
        requiered_field(document.forms["registerform"].email) &&
        requiered_field(document.forms["registerform"].b_address) &&
        requiered_field(document.forms["registerform"].b_city) &&
        requiered_field(document.forms["registerform"].b_state) &&
        requiered_field(document.forms["registerform"].b_country) &&
        requiered_field(document.forms["registerform"].b_zipcode) &&
        checkEmailAddress(document.forms["registerform"].email)
        && check_zip_code())
    document.forms["registerform"].submit();
}

function check_custom_register_form()
{
    //$("#register_form").
    
    if (requiered_field(document.forms["registerform"].p_describe) &&
        requiered_field(document.forms["registerform"].p_painter) &&
        requiered_field(document.forms["registerform"].p_size) &&
        requiered_field(document.forms["registerform"].title) &&
        requiered_field(document.forms["registerform"].name) &&
        requiered_field(document.forms["registerform"].last_name) &&
        requiered_field(document.forms["registerform"].phone) &&
        requiered_field(document.forms["registerform"].email) &&
        requiered_field(document.forms["registerform"].b_address) &&
        requiered_field(document.forms["registerform"].b_city) &&
        requiered_field(document.forms["registerform"].b_state) &&
        requiered_field(document.forms["registerform"].b_country) &&
        requiered_field(document.forms["registerform"].b_zipcode) &&
        requiered_field(document.forms["registerform"].p_amount) &&
        checkEmailAddress(document.forms["registerform"].email)
        && check_zip_code())
    document.forms["registerform"].submit();
}

function check_contactus_form()
{
    //$("#register_form").
    
    if (requiered_field(document.forms["contactus"].name) &&
        requiered_field(document.forms["contactus"].email) &&
        requiered_field(document.forms["contactus"].message) &&
        //requiered_field(document.forms["contactus"].captcha) &&
        checkEmailAddress(document.forms["contactus"].email)
        )
    document.forms["contactus"].submit();
    else 
    {
	alert('Please check required fields');
	return false;
    }
}


function requiered_field(field)
{
    if (field.value == '')
    {
        field.focus();
        field.select();
        return false;
    }
    else
    return true;
}

function checkEmailAddress(field) {
    var goodEmail = field.value.search(/^[^\.]*[A-Za-z0-9_\-\.]*[^\.]\@[^\.][A-Za-z0-9_\-\.]+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\.info)|(\.biz)|(\.us)|(\.bizz)|(\.coop)|(\..{2,2}))[ ]*$/gi);

    if (goodEmail!=-1) 
    {
        return true;
    }
    else 
    {
        alert("E-mail address is invalid! Please correct.");
        field.focus();
        field.select();
        return false;
    }
}

function check_zip_code_field(cnt, zip){
    var country = cnt.options[cnt.selectedIndex].value;
    if (country=="US")
    {
        if (zip.value.length!=5 && zip.value!="") 
        {
            alert("Ensure that you have 5 digits in your zip code");
            zip.focus();
            return false;
        }
    }
    else if (country=="CA") 
    {
        if (zip.value.length!=6 && zip.value.length!=7 && zip.value!="")
        {
            alert("Ensure that you have 6 or 7 characters in your postal code");
            zip.focus();
            return false;
        }
    }
return true
}

function check_zip_code(){
    return check_zip_code_field(document.forms["registerform"].b_country, document.forms["registerform"].b_zipcode);
}

function add_to_wishlist_cart(productid)
{
    $("#wishlist_head").highlightFade("#c69b3c");
    $("#wishlist").load("/wishlist/add/"+productid, '',
        function(){
            //$("#wishlist_head").highlightFade();
        });
    $("#cs_main_content").load("/cart/move_to_wishlist/"+productid, '',
        function(){
            //$("#wishlist_head").highlightFade();
        });
}
// -------------------------------------------------------------------------
function init()
{
	$("div[class='mh_item_content_float']").hover(
    function() {  
    $(this).addClass('hover');
    },  
    function() {  
    $(this).removeClass('hover');  
    }  
    );
    lang_init();
    $("#sizes_popup").hide();
    $("#loading_container").click(function () { 
      //$("#loading_container").fadeOut("fast");
      $("#loading_container").hide();
      $("#sizes_popup").hide();
    });
}

function getPageSize(){
 var xScroll, yScroll, windowWidth, windowHeight, b = document.body, de = document.documentElement;
 if (window.innerHeight && window.scrollMaxY) {
  xScroll = b.scrollWidth;
  yScroll = window.innerHeight + window.scrollMaxY;
 } else if (b.scrollHeight > b.offsetHeight){ // all but Explorer Mac
  xScroll = b.scrollWidth;
  yScroll = b.scrollHeight;
 } else if (de && de.scrollHeight > de.offsetHeight){ // Explorer 6 strict mode
  xScroll = de.scrollWidth;
  yScroll = de.scrollHeight;
 } else { // Explorer Mac...would also work in Mozilla and Safari
  xScroll = b.offsetWidth;
  yScroll = b.offsetHeight;
 }

 if (self.innerHeight) { // all except Explorer
  windowWidth = self.innerWidth;
  windowHeight = self.innerHeight;
 } else if (de && de.clientHeight) { // Explorer 6 Strict Mode
  windowWidth = de.clientWidth;
  windowHeight = de.clientHeight;
 } else if (b) { // other Explorers
  windowWidth = b.clientWidth;
  windowHeight = b.clientHeight;
 }

 // for small pages with total height less then height of the viewport
 var pageHeight = yScroll < windowHeight? windowHeight : yScroll;

 // for small pages with total width less then width of the viewport
 var pageWidth = xScroll < windowWidth? windowWidth : xScroll;

 //return [pageWidth,pageHeight,windowWidth,windowHeight]
 return pageHeight;
}


function getWinWidth(){
 var xScroll, yScroll, windowWidth, windowHeight, b = document.body, de = document.documentElement;
 if (window.innerHeight && window.scrollMaxY) {
  xScroll = b.scrollWidth;
  yScroll = window.innerHeight + window.scrollMaxY;
 } else if (b.scrollHeight > b.offsetHeight){ // all but Explorer Mac
  xScroll = b.scrollWidth;
  yScroll = b.scrollHeight;
 } else if (de && de.scrollHeight > de.offsetHeight){ // Explorer 6 strict mode
  xScroll = de.scrollWidth;
  yScroll = de.scrollHeight;
 } else { // Explorer Mac...would also work in Mozilla and Safari
  xScroll = b.offsetWidth;
  yScroll = b.offsetHeight;
 }

 if (self.innerHeight) { // all except Explorer
  windowWidth = self.innerWidth;
  windowHeight = self.innerHeight;
 } else if (de && de.clientHeight) { // Explorer 6 Strict Mode
  windowWidth = de.clientWidth;
  windowHeight = de.clientHeight;
 } else if (b) { // other Explorers
  windowWidth = b.clientWidth;
  windowHeight = b.clientHeight;
 }

 // for small pages with total height less then height of the viewport
 var pageHeight = yScroll < windowHeight? windowHeight : yScroll;

 // for small pages with total width less then width of the viewport
 var pageWidth = xScroll < windowWidth? windowWidth : xScroll;

 //return [pageWidth,pageHeight,windowWidth,windowHeight]
 return pageWidth;
}



function getWinSize(){
 var xScroll, yScroll, windowWidth, windowHeight, b = document.body, de = document.documentElement;
 if (window.innerHeight && window.scrollMaxY) {
  xScroll = b.scrollWidth;
  yScroll = window.innerHeight + window.scrollMaxY;
 } else if (b.scrollHeight > b.offsetHeight){ // all but Explorer Mac
  xScroll = b.scrollWidth;
  yScroll = b.scrollHeight;
 } else if (de && de.scrollHeight > de.offsetHeight){ // Explorer 6 strict mode
  xScroll = de.scrollWidth;
  yScroll = de.scrollHeight;
 } else { // Explorer Mac...would also work in Mozilla and Safari
  xScroll = b.offsetWidth;
  yScroll = b.offsetHeight;
 }

 if (self.innerHeight) { // all except Explorer
  windowWidth = self.innerWidth;
  windowHeight = self.innerHeight;
 } else if (de && de.clientHeight) { // Explorer 6 Strict Mode
  windowWidth = de.clientWidth;
  windowHeight = de.clientHeight;
 } else if (b) { // other Explorers
  windowWidth = b.clientWidth;
  windowHeight = b.clientHeight;
 }

 // for small pages with total height less then height of the viewport
 var pageHeight = yScroll < windowHeight? windowHeight : yScroll;

 // for small pages with total width less then width of the viewport
 var pageWidth = xScroll < windowWidth? windowWidth : xScroll;

 //return [pageWidth,pageHeight,windowWidth,windowHeight]
 return windowHeight;
}

function tell_a_friend()
{
    $.blockUI({ message: $('#tell_a_friend_container')});
}


function send_ecart()
{
    $.blockUI({ message: $('#send_ecart')});
}

function send_ecart_catalogue(artist, painting, thumb, id)
{
    $("img.ecart_painting_img").attr("src", thumb);
    $("p.ecart_painting").html(artist+'<br/>'+painting);
    $("#send_ecart_id").val(id);
    $.blockUI({ message: $('#send_ecart')});
}



function signin()
{
    //$('#wishlist_login input[@name=login]').val = $('#signin_form_container input[@name=login]').val;
    //$('#wishlist_login input[@name=pass]').val = $('#signin_form_container input[@name=pass]').val;
    //$('#wishlist_login').submit(); 
	$('#signin_form_container').submit();
}

function show_hide_wishlist(maximize_str, minimize_str)
{
 wishlist_div = document.getElementById('wishlist');
 if (wishlist_div != null)
 {
  if (wishlist_div.style.display != 'block')
  {
    //$("#wishlist").css("display", "block");
    $("#wishlist").slideDown("medium");
    Set_Cookie('max_wishlist', 1, 0, "/");
    $("#footer").css("padding","0 0 163px 0");
    /*$("#frame").css("margin","0 0 120px 0");*/
    $("body").css("padding","0 0 180px 0");
    $("a.wishlist_button").css("background-position", "bottom left");
    $("a.wishlist_button").html(minimize_str);
  }
  else
  {
    //$("#wishlist").css("display", "none");
    $("#wishlist").slideUp(1000);
    Set_Cookie('max_wishlist', 0, 0, "/");
    $("#footer").css("padding","0 0 34px 0");
    $("#frame").css("margin","0 0 0 0");
    $("body").css("padding","0 0 50px 0");
    $("a.wishlist_button").css("background-position", "bottom right");
    $("a.wishlist_button").html(maximize_str);
  }
 }
}

function show_wishlist(minimize_str)
{
 wishlist_div = document.getElementById('wishlist');
    //$("#wishlist").css("display", "block");
    $("#wishlist").slideDown(1000);
    Set_Cookie('max_wishlist', 1, 0, "/");
    $("#footer").css("padding","0 0 163px 0");
    $("#frame").css("margin","0 0 120px 0");
    $("#show_hide_wishlist > a").css("background-position", "top right");
    $("#show_hide_wishlist > a").html(minimize_str);
}













function Set_Cookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

if (name=='show_perpage')
path = '/';

/*
if the expires variable is set, make the correct
expires time, the current script below will set
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}

function Get_Cookie( check_name ) {
 // first we'll split this cookie up into name/value pairs
 // note: document.cookie only returns name=value, not the other components
 var a_all_cookies = document.cookie.split( ';' );
 var a_temp_cookie = '';
 var cookie_name = '';
 var cookie_value = '';
 var b_cookie_found = false; // set boolean t/f default f

 for ( i = 0; i < a_all_cookies.length; i++ )
 {
  // now we'll split apart each name=value pair
  a_temp_cookie = a_all_cookies[i].split( '=' );


  // and trim left/right whitespace while we're at it
  cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

  // if the extracted name matches passed check_name
  if ( cookie_name == check_name )
  {
   b_cookie_found = true;
   // we need to handle case where cookie has no value but exists (no = sign, that is):
   if ( a_temp_cookie.length > 1 )
   {
    cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
   }
   // note that in cases where cookie is initialized but no value, null is returned
   return cookie_value;
   break;
  }
  a_temp_cookie = null;
  cookie_name = '';
 }
 if ( !b_cookie_found )
 {
  return null;
 }
}




// ---------------------------------------------------------------------------
function lang_init() {
    $("div.header_menu ul li").mouseover(function(){
      $("#header_location_pop_up").css("display", "block");
    }).mouseout(function(){
      $("#header_location_pop_up").css("display", "none");
    });

}
function set_language(lang) {
    Set_Cookie('lang', lang, 365, "/");
}
// ---------------------------------------------------------------------------




// ---------------------------------------------------------------------------
function toggle_catalogue(index, el) {
    var count_child = el.parentNode.parentNode.getElementsByTagName('li').length;
    for (var i = 0; i < count_child; i++)
		{
		el.parentNode.parentNode.getElementsByTagName('li')[i].className = '';
		var j = i+1;
        document.getElementById('chm_catalogue_page'+j).style.display = 'none';
		}
    el.parentNode.className = 'current';
    document.getElementById('chm_catalogue_page'+index).style.display = 'block';
}
// ---------------------------------------------------------------------------


function show_sizes(productid, parent_href)
{
    sizes_div = document.getElementById('sizes_popup_'+productid);
    sizes_div.style.display = 'block';
    sizes_div.style.top = findPosY(parent_href)-80+'px';
    sizes_div.style.left = findPosX(parent_href)-50+'px';
}

function change_price(select_elem, productid)
{
    size_span = document.getElementById('size_'+productid);
    price_select_text = select_elem.options[select_elem.selectedIndex].text;
    if (size_span != null && size_span != 'undefined')
    size_span.innerHTML = substr(price_select_text,0,strpos(price_select_text," "));
    price_span = document.getElementById('pricebox_'+productid);
    price_span.innerHTML = select_elem.options[select_elem.selectedIndex].value;// + '.00';
    sizes_div = document.getElementById('sizes_popup_'+productid);
    if (sizes_div != null && sizes_div != 'undefined')
    sizes_div.style.display = 'none';
}

function change_price_updated(price_text, size_key, productid)
{
    size_span = document.getElementById('size_'+productid);
    
    size_text = $("#size_a_label_"+productid+"_"+size_key).html();
    
    $("input[name='size_price']").val(size_key);
    
    if (size_span != null && size_span != 'undefined')
    size_span.innerHTML = substr(size_text,0,strpos(size_text," "));
    
    price_span = document.getElementById('pricebox_'+productid);
    //price_span.innerHTML = select_elem.options[select_elem.selectedIndex].value;// + '.00';
    price_span.innerHTML = price_text;// + '.00';
    sizes_div = document.getElementById('sizes_popup_'+productid);
    if (sizes_div != null && sizes_div != 'undefined')
    sizes_div.style.display = 'none';

    $("#unit_font").show();
}

function change_price_catalogue_updated(price_text, productid, size_key)
{
    price_span = document.getElementById('pricebox_'+productid);
    price_span.innerHTML = price_text;// + '.00';
    sizes_div = document.getElementById('sizes_popup_'+productid);
    if (sizes_div != null && sizes_div != 'undefined')
    sizes_div.style.display = 'none';
    
    sizes_select = document.getElementById('sizes_'+productid);
    sizes_select.options[size_key].selected = true;
    
    size_span = document.getElementById('size_'+productid);
    size_span.innerHTML = substr(sizes_select.options[size_key].text,0,strpos(sizes_select.options[size_key].text," "));;
}

function submit_to_cart_separate()
{
    cartform = document.getElementById('cart_form');
    if (cartform != null && cartform != 'undefined')
    {
        //cartform.size_price.value = cartform.size.value;
        if ($("#radio_custom").attr("checked") == true)
        $("#size_price_input").val('');
        
        cartform.submit();
    }
}

function submit_to_cart_wishlist(productid)
{
    cartform = document.getElementById('wishlist_cart_form');
    if (cartform != null && cartform != 'undefined')
    {
        cartform.productid.value = productid;
        cartform.submit();
    }
}

function submit_to_cart_catalogue(productid)
{
    cartform = document.getElementById('cart_form');
    if (cartform != null && cartform != 'undefined')
    {
        sizes_select = document.getElementById('sizes_'+productid);
        cartform.size_price.value = sizes_select.selectedIndex;
        cartform.productid.value = productid;
        cartform.submit();
    }
}

function close_sizes(element)
{
    element.parentNode.style.display = 'none';
}

function findPosX(obj)
{
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
}
function findPosY(obj)
{
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
}

function getWinHeight()
{
    winH = 600;
    if (typeof window.innerWidth != 'undefined')
    {
        winH = window.innerHeight
    }
    else if (typeof document.documentElement != 'undefined'
            && typeof document.documentElement.clientWidth !=
            'undefined' && document.documentElement.clientWidth != 0)
    {
        winH = document.documentElement.clientHeight
    }
    else
    {
        winH = document.getElementsByTagName('body')[0].clientHeight
    }
    return winH;
}

function show_painting_cart(productid, parent_href)
{
    painting_cart = document.getElementById('shop_cart_popup_'+productid);
    painting_cart.style.display = 'block';
    painting_cart.style.top = findPosY(parent_href)-60+'px';
    painting_cart.style.left = findPosX(parent_href)-30+'px';
}
function select_painting_for_framing(frameid, product_name, productid, elem)
{
    selected_painting_span = document.getElementById('selected_painting_'+frameid);
    selected_painting_span.innerHTML = product_name;
    selected_painting_span.parentNode.style.display = 'block';
    painting_cart = document.getElementById('shop_cart_popup_'+frameid);
    painting_cart.style.display = 'none';
    painting_id = document.getElementById('selected_painting_id_'+frameid);
    painting_id.value = productid;
    framing_add_to_cart = document.getElementById('framing_add_to_cart_'+frameid);
    framing_add_to_cart.style.display = 'block';
}
function submit_to_cart_frame_catalogue(frameid)
{
    cartform = document.getElementById('cart_form');
    if (cartform != null && cartform != 'undefined')
    {
        sizes_select = document.getElementById('sizes_'+frameid);
        cartform.size_price.value = sizes_select.selectedIndex;
        cartform.productid.value = frameid;
        painting_id = document.getElementById('selected_painting_id_'+frameid);
        cartform.for_product.value = painting_id.value;
        //return false;
        cartform.submit();
    }
}
function strpos( haystack, needle, offset){
    var i = haystack.indexOf( needle, offset ); // returns -1
    return i >= 0 ? i : false;
}
function substr( f_string, f_start, f_length ) {
    if(f_start < 0) {
        f_start += f_string.length;
    }
 
    if(f_length == undefined) {
        f_length = f_string.length;
    } else if(f_length < 0){
        f_length += f_string.length;
    } else {
        f_length += f_start;
    }
 
    if(f_length < f_start) {
        f_length = f_start;
    }
 
    return f_string.substring(f_start, f_length);
}
