var searchUrl;

function  addtoUrl(opt)
    {
    ddd = $(opt);
    val = ddd.value;
    re = new RegExp("/", "g")

    if (val != '')
        {
        searchUrl = searchUrl + '/' + opt + '/' + val.replace(re, '-')
        }
    }

function  addtoUrl2(param, opt)
    {
    ddd = $(opt);
    val = ddd.value;
    re = new RegExp("/", "g") ;
    sp = new RegExp(" ", "g") ;

    if (val != '')
        {
        p=val.replace(re, '-');
        p=p.replace(sp, '+');    
        searchUrl = searchUrl + '/' + param + '/' + p
        }
    }

function SubmitSearch()
    {
    searchUrl = env+'/search';
    addtoUrl('category');
    addtoUrl('producttype');
    addtoUrl('creator');
    addtoUrl('material1');
    addtoUrl('material2');
    addtoUrl('afterdate');
    addtoUrl('beforedate');
    addtoUrl('productid');
    addtoUrl2('keywords', 'searchkeyword');
    window.document.location.href = searchUrl;
    }

function ClearSearch()
    {
    $('category').selectedIndex = 0;
    $('producttype').selectedIndex = 0;
    $('creator').selectedIndex = 0;
    $('material1').selectedIndex = 0;
    $('material2').selectedIndex = 0;
    s = $('beforedate').value = '';
    s = $('afterdate').value = '';
    s = $('productid').value = '';
    s = $('searchkeyword').value = '';
    }

function SubmitKeywordSearch()
    {
    searchUrl = env+'/find';
    addtoUrl2('keywords','keywords');
    window.document.location.href = searchUrl;
    }

function basketremove(id)
    {
    window.document.location.href = env+'/basket/remove?id=' + id;
    }

function wishlistremove(id)
    {
    window.document.location.href = env+'/wishlist/remove?id=' + id;
    }

function movetobasket(id)
    {
    window.document.location.href = env+'/wishlist/move?id=' + id;
    }

function movetowishlist(id)
    {
    window.document.location.href = env+'/basket/move?id=' + id;
    }
    
    
function roomsetselect(ck,id)
{    
if (ck.checked) action='add';
else action='delete';
jQuery.get(env+"/roomsets/product/update", {selectaction: action,id: id } );    
}



function downloadremove(id)
    {
    if (confirm('Are you Sure?'))
        {
        window.document.location.href = env+'/user/remove/id/' + id;
        }
    }

function downloadmodel(id,currentpage)
    {
    //window.document.location.href='/main_test.php/user/download/id/'+id;
    //window.document.location.href='/main_win.php/user/download/id/'+id;
    href = env+'/user/download/id/' + id;
    if (currentpage) href=href+'?currentpage='+currentpage;
    window.document.location.href=href; 
    }

function addtowishlist()
    {
    form = $('basketform');
    form.action = env+'/wishlist/add';
    form.submit();
    return false;
    }
    
function addtobasket()
    {
    form = $('basketform');
    form.action = env+'/basket/add';
    form.submit();
    return false;
    }

    

function addedtobasket(request)
    {
    ad = $('addedtext');

    if (request.responseText > 0)
        {
        ad.addClassName('colourhighlight');
        $('basketcount').innerHTML = '[' + request.responseText + ']'
        ad.innerHTML = 'You have successfully added this item to your basket';
        }

    else
        {
        if (request.responseText == -1)
            {
            ad.addClassName('colourhighlight');
            ad.innerHTML = 'This item is already in your basket';
            }
        }
    }

function addedtowishlist(request)
    {
    ad = $('addedtext');

    if (request.responseText > 0)
        {
        ad.addClassName('colourhighlight');
        $('wishlistcount').innerHTML = '[' + request.responseText + ']'
        ad.innerHTML = 'You have successfully added this item to your wishlist';
        }

    else
        {
        if (request.responseText == -1)
            {
            ad.addClassName('colourhighlight');
            ad.innerHTML = 'This item is already in your wishlist';
            }
        }
    }

function wishlistover()
    {
    $('wishlistcount').toggleClassName('whitetext');
    MM_swapImage('wishlistimg', '', '/images/layout/topbar/wishlist-white.png', 1);
    }

function wishlistout()
    {
    $("wishlistcount").toggleClassName("whitetext");
    MM_swapImgRestore();
    }

function basketover()
    {
    $('basketcount').toggleClassName('whitetext');
    MM_swapImage('basketimg', '', '/images/layout/topbar/basket-white.png', 1);
    }

function basketout()
    {
    $('basketcount').toggleClassName('whitetext');
    MM_swapImgRestore();
    }


function logout(confirm)
    {
    if (confirm == 1)
        {
        document.logoutform.submit()
        }

    else
        {
        history.go(-1);
        }
    }

function unScramble(eMail1, eMail2, linkText, subjectText, statusText)
    {
    var a, b, c, d, e;
    a = eMail1;
    c = linkText;
    b = eMail2.substring(0, eMail2.length - 5);

    if (subjectText != "")
        {
        d = "?subject=" + escape(subjectText);
        }

    else
        {
        d = "";
        }

    if (statusText != "")
        {
        e = " onMouseOver=\"top.status=\'" + statusText +
            "\'\;return true\;\" onMouseOut=\"top.status=\'\'\;return true\;\"";
        }

    else
        {
        e = "";
        }
    document.write("<A class=\"link\" HREF=\"mai" + "lto:" + a + "@" + b + d + "\"" + e + ">" + c + "</A>");
    }

function email()
    {
    unScramble(String.fromCharCode(105, 110, 102, 111), "mx-3dmodels.complete",
        'info@<span class="emaila">(remove)</span><span class="emailb">mx-3dmo</span>dels.com', "", "");
    }

function supportemail()
    {
    unScramble(String.fromCharCode(115, 117, 112, 112, 111, 114, 116), "mx-3dmodels.complete",
        'support@<span class="emaila">(remove)</span><span class="emailb">mx-3dmo</span>dels.com', "", "");
    }

function submitenter(myfield, e)
    {
    var keycode;

    if (window.event)
        keycode = window.event.keyCode;

    else if (e)
        keycode = e.which;

    else
        return true;

    if (keycode == 13)
        {
        SubmitKeywordSearch();
        return false;
        }

    else
        return true;
    }
    
var current_image=1;
var current_large_image=1;

function show_image(newimage_no)
{
if (newimage_no==current_image) return;
if (newimage_no==0){show_rotate();return;}
jQuery('#playerdiv').addClass('hidden');   
jQuery('#playerdiv').removeClass('visible');  
newimage=image_list[newimage_no-1];
jQuery('#imagediv').attr('src',newimage);
jQuery('#image_link_'+current_image).removeClass('active');
jQuery('#image_link_'+newimage_no).addClass('active');
jQuery('#imagediv').show();
current_image=newimage_no;
}

function  next_image(inc,max)
{
if (current_image==0) current_image=1;    
newimage_no=current_image+inc;
if ((newimage_no) < 1) return;
if ((newimage_no) > max) return;
show_image(newimage_no);

}

function show_rotate()
{
jQuery('#imagediv').hide();
jQuery('#playerdiv').addClass('visible');   
jQuery('#playerdiv').removeClass('hidden');   
jQuery('#image_link_'+current_image).removeClass('active');
jQuery('#image_link_0').addClass('active');

}

function getadvtype(catselector)
{
url=env+'/getcattypes/'+catselector.value;
type=$('producttype');
cleartypes(type);
new Ajax.Request(url,
  {
  method: 'post',
  onSuccess: function(transport)
   {
   JSONdata=transport.headerJSON;

   add2types(type,'','')
   for (i =0; i<JSONdata.length; i++)
    {
    add2types(type,JSONdata[i][0],JSONdata[i][1])
    }
   }   
  }
  )
}

function cleartypes(type)
{
   var i;
   for (i = type.length - 1; i>=0; i--)
    {
    type.remove(i);
    }    
    
}

function add2types(type,opttext,val)
{
var elOptNew = document.createElement('option');
elOptNew.text = opttext;
elOptNew.value = val;
try {
     type.add(elOptNew, null); // standards compliant; doesn't work in IE
    }
   catch(ex) {
     type.add(elOptNew); // IE only   
     }
}

function fixIE(height,over)
{
    bod = document.getElementsByTagName('body')[0];
    bod.style.height = height;
    bod.style.overflow = over;

    htm = document.getElementsByTagName('html')[0];
    htm.style.height = height;
    htm.style.overflow = over;
}


function showRoomlarge()
{
// fixIE('100%', 'hidden');
 window.scrollTo(0, 0);
 var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }

 i=$('browseinner');
 w=1140;
 x=(myWidth-w)/2;
 x=Math.max(x,0);
jQuery('#playerdiv').addClass('hidden');   
jQuery('#playerdiv').removeClass('visible');  
 $('browseouter').setStyle({left : '0px'});
 $('browseinner').setStyle({paddingLeft:x+'px',left:'0px'});
 $('browseinner').show();
}

function HideRoomlarge()
{
// fixIE('100%', 'hidden');
if (current_roomset==original_roomset)
    {
    window.scrollTo(0, 0);
    $('browseouter').style.left='-999999px';
    $('browseinner').style.left='-999999px';  
    }
else
    {
     window.document.location.href = roomset_list[current_roomset-1]; 
     }
}



function show_roomset(newimage_no)
{
if (newimage_no==current_roomset) return;
newimage=roomsetimage_list[newimage_no-1];
jQuery('#browseimage').attr('src',newimage);
jQuery('#roomset_link_'+current_roomset).removeClass('active');
jQuery('#roomset_link_'+newimage_no).addClass('active');
jQuery('#imagediv').show();
current_roomset=newimage_no;
}

function  next_roomset(inc,max)
{
newimage_no=current_roomset+inc;
if ((newimage_no) < 1) return;
if ((newimage_no) > max) return;
show_roomset(newimage_no);

}



function show_large(newimage_no)
{
if (newimage_no==current_large_image) return;
newimage=image_list[newimage_no-1];

jQuery('#browseimage').attr('src',newimage);
jQuery('#large_image_link_'+current_large_image).removeClass('active');
jQuery('#large_image_link_'+newimage_no).addClass('active');
jQuery('#imagediv').show();
current_large_image=newimage_no;
}

function  next_large(inc,max)
{
if (current_large_image==0) current_large_image=1;    
newimage_no=current_large_image+inc;
if ((newimage_no) < 1) return;
if ((newimage_no) > max) return;
show_large(newimage_no);

}

function Hidelarge()
{
// fixIE('100%', 'hidden');
window.scrollTo(0, 0);
$('browseouter').style.left='-999999px';
$('browseinner').style.left='-999999px'; 
show_image(1);     
}

