﻿$(document).ready(function(){
    	
    //apply ifixpng	
    $('.display-s-offer').ifixpng();
    $('.display-out-of-stock').ifixpng();
    $('.item-thumb').ifixpng();
    
    //show submenus
    
    //$('#menu li').find('div').hide().end().find('a[@href*=#], a.more').click(function(){  
    $('#menu li').find('a[@href*=#], a.more').click(function(){        
        var smenu = $(this).next();        
        if(smenu.is(':visible')){
            smenu.slideUp();
        }else{
            smenu.slideDown();
        }
        return false
    });
    
    //itens home
    $('ul.list-products li div a').click(function(){
        var id = $(this).attr('id');
        $('div#menu ul li a').next('div').hide();
        if(id == 'p_tile'){
            $('div#menu ul li a.mnl-tiles').next('div').show();
        }else if(id == 'p_ceramic'){
            $('div#menu ul li a.mnl-stone-products').next('div').show();
        }else if(id == 'p_woodflooring'){
            $('div#menu ul li a.mnl-wood-flooring').next('div').show();
        }else if(id == 'p_complementary'){
            $('div#menu ul li a.mnl-complementary').next('div').show();
        }else if(id == 'p_worktops'){
            $('div#menu ul li a.mnl-worktops').next('div').show();
        }
        return false;
    })
    
    //exibir div prices
    $('td.col-price div a.lnk-act').click(function(){
        var prices = $(this).next();
        if (prices.is(':hidden')){
            prices.show('fast');
        }
        return false;
    }); 
    
    $('div.all-prices a').click(function(){
        var prices = $(this).parent();
        if (prices.is(':visible')){
            prices.hide('fast');
        }
        return false;
    });
    
    //Tabela de preços
    
    $('#tb-product-info .quantity').keypress(function(event){
        if (event.charCode && (event.charCode < 48 || event.charCode > 57))
        {
        event.preventDefault();
        }
    });
    
    /*
    $('#tb-product-info .quantity').change(function() {
        var totalQuantity = 0;
        $('#tb-product-info .quantity').each(function() {
            var quantity = parseInt(this.value);
            totalQuantity += quantity;
        });
        $('#tb-product-info .total-value').html('£ '+parseInt(Number(totalQuantity))+' / m²');
    }); 
    */
    
    //Highlight Rows
    
    $('.tb-base tbody tr').hover(function(){
        $(this).addClass('hightlight');
        }, function(){
        $(this).removeClass('hightlight');
        });
        
/*    $('.tb-base tbody tr a.o_num').each(function(i){
        var link = $(this).attr("href");
        $(this).parent('td').parent('tr').hover(function(){
            $(this).addClass('hightlight');
            $(this).click(function(){
                window.location.href = link;
            });        
        }, function(){
            $(this).removeClass('hightlight');});
        });*/
        
    //Image Galery of Products
    
    //Modal    
    $('.img-placeholder').each(function(){
        var img = $(this).attr('lowsrc');
        if(img != " " && img != null && img != "undefined" && img != '#' ){
            $(this).css('cursor','pointer').prev('a').addClass('expand');
            $('a.expand').attr({'title':'Click on the image to view a large size version'}).click(function(){return false;});
        }     
    });
    
    $('#box-galery-thumbs a').each(function(i){        
        $(this).click(function(){
            var source = $(this).find('img').attr("src");
            var wide = $(this).attr("href");
            var text = $(this).attr("title");
            if(wide != '' && wide != 'undefined'&& wide != '#' ){
                $('.img-placeholder').attr({'src':source,'alt':text,'lowsrc':wide}).css('cursor','pointer').prev('a').addClass('expand');
                $('a.expand').attr({'title':'Click on the image to view a large size version'}).click(function(){return false;});
            }else{
                $('.img-placeholder').attr({'src':source,'alt':text,'lowsrc':wide}).css('cursor','default').prev('a').removeClass('expand');
            }            
            return false;
        });
    }); 
    
    //Modal    
    $('.img-placeholder').click(function(){
        var img = $(this).attr('lowsrc');
        if(img != '' && img != 'undefined'&& img != '#' ){
            var text = $(this).attr('alt');
            $.modal('<img src='+img+' width="814" height="475" alt='+text+'/><p>'+text+'</p>');
        }        
    });
    
    // Contact
    
    $('.form-contact select').bind('change', function(){
        var subject = $(this).val();
        var para = $('#downThis');
        if(para){
            $(para).remove();
        };
        if(subject == 'catalogue'){
            $('<p id="downThis">You can also <a href="download catalogue in pdf" title="Catalogue in pdf">download catalogue in .pdf(4184kb).</a></p>').insertAfter('div.fr');
        };
    });
    
    //Função para inserir novo endereço no form Sign UP
    
    $('a[@rel=newaddr]').click(function(){
        
        var n=1;    
        var newaddr = '<br /><div class=\"cl\"><label>Address:</label><asp:TextBox ID=\"txtAddress'+n+'\" runat=\"server\" CssClass=\"size\3" /></div>';
        newaddr += '<div><label>Citi/Town:</label><asp:TextBox ID=\"txtCiti'+n+'\" runat=\"server\" CssClass=\"size3\" /></div>';
        newaddr += '<br />';
        newaddr += '<div class=\"cl\"><label>Country/Region</label><asp:dropdownList ID=\"ddlCountry'+n+'\" runat=\"server\" CssClass=\"size2\">';
        newaddr += '<asp:ListItem Text=\"England\" Value=\"\" />';
        newaddr += '</asp:dropdownList></div>';
        newaddr += '<div><label>Post Code:</label><asp:TextBox ID=\"txtpostCode'+n+'\" runat=\"server\" CssClass=\"size1\" /></div>';

        $('a[@rel=newaddr]').parent('p').before(newaddr);
        
        n++;
        return false;
    });
    
    //Script para alterar titulo Menu About, Menu Sections e Institucional
    
    var currentUrl = window.location.href;
    
    //Seção About
    
    if (currentUrl.indexOf('about-Arthome.aspx')!= -1){
        $('#menu h2').addClass('tit-mn-about-Arthome').html('ABOUT ArtHome');
        $('a.mnl-about').addClass('on');
        $('ul.sections li a.mn-about-Arthome').addClass('on');
    }else if(currentUrl.indexOf('trade-sales.aspx')!= -1){
        $('#menu h2').addClass('tit-mn-trade-sales').html('TRADE SALES');
        $('a.mnl-trade-sales').addClass('on');
        $('ul.sections li a.mn-about-Arthome').addClass('on');
    }else if(currentUrl.indexOf('catalogue.aspx')!= -1){
        $('#menu h2').addClass('tit-mn-catalogue').html('CATALOGUE REQUEST');
        $('a.mnl-catalogue').addClass('on');
        $('ul.sections li a.mn-about-Arthome').addClass('on');
    }else if(currentUrl.indexOf('contact.aspx')!= -1){
        $('#menu h2').addClass('tit-mn-contact').html('CONTACT US');
        $('a.mn-contact-us').addClass('on');
        $('a.mnl-contact').addClass('on');
        $('ul.sections li a.mn-about-Arthome').addClass('on');
    }else if(currentUrl.indexOf('sitemap.aspx')!= -1){
        $('#menu h2').addClass('tit-mn-sitemap').html('SITEMAP');
        $('a.mn-site-map').addClass('on');
        $('a.mnl-sitemap').addClass('on');
        $('ul.sections li a.mn-about-Arthome').addClass('on');
    }
    
    //Seção Products
    
    if (currentUrl.indexOf('/products/product')!= -1){
        $('ul.sections li a.mn-products').addClass('on');
    }else if (currentUrl.indexOf('/products/special')!= -1){
        $('ul.sections li a.mn-special-offers').addClass('on');
    }
    
    //Sign in / Sign up
    
    if (currentUrl.indexOf('/sign/')!= -1){
        $('a.mn-sign-in').addClass('on');
        $('a.mn-sign-up').addClass('on');
    }
    
     //My Order
    
    if (currentUrl.indexOf('/myorder/')!= -1){
        $('a.mn-my-order').addClass('on');
    }
    
    //Search
    
    if (currentUrl.indexOf('/search-products.aspx')!= -1){
        $('a.mnl-search-products').addClass('on');
    }else if (currentUrl.indexOf('/search-content.aspx')!= -1){
        $('a.mnl-search-content').addClass('on');
    }
    
        
    /*Backgrond transparent */
    $('<div class="transparent"><!-- --></div>').appendTo('div#header');
    $('div.transparent').css('opacity',0.8);
        
});

// Carrocel

var margin = 0;

function next(){
    var id = "box-galery-thumbs";
    var container = document.getElementById(id);
    var thumbs=document.getElementById(id).getElementsByTagName('img').length;
    container.style.width = (thumbs * 65)+"px"; 
    var deslocamento = parseInt(thumbs-4) * 63;
    if (margin >= -(deslocamento)&& thumbs>4) {
        margin -= 65;
        document.getElementById(id).style.left = margin + "px";
    }
}
 
function prev(){
    var id = "box-galery-thumbs";
    if (margin != 0) {
        margin += 65;
        document.getElementById(id).style.left = margin + "px";
    }
}