
//<script type="text/javascript">

 

Roo.onReady(function() {
    try {
         selectItem();
    } catch (e) { 
    }
    
    if (Roo.select('.body-area-right-product-image img')) {
        initProductImages();
    }
    if (document.getElementById('tweet')) {
                    
        getTwitters('tweet', {
          id: 'BGasia',
          count: 5,
          enableLinks: true,
          ignoreReplies: true,
          clearContents: true,
          template: '%text%'
        });
      }
    
    Roo.select('.more-info-heading',true).on('click', function(e) {
        e.preventDefault();
        Roo.select('.more-info-expand',true).toggle();
        Roo.select('.more-info-collapse',true).toggle();

        Roo.select('.more-info-box',true).toggle();
    });
    Roo.select('.more-info-box',true).setVisibilityMode(Roo.Element.DISPLAY);
    Roo.select('.more-info-expand',true).setVisibilityMode(Roo.Element.DISPLAY);
    
},true);

var pimage = false;
var lightbox = false;
var image_css = false;
var image_mask = false;

function initProductImages()
{
    pimage = Roo.select('.body-area-right-product-image img', true).elements[0];
    if (!pimage) {
        return;
    }
    var lightbox_image_div =  Roo.DomHelper.append(document.body, {
        id : Roo.id,
        style : 'display:none'
    },true);
    
    
    Roo.select('.body-area-right-product-image img', true).on('load', function() {
        image_mask.hide();
        this.fadeIn({
            endOpacity: 0, //can be any value between 0 and 1 (e.g. .5)
            easing: 'easeIn',
            duration: .2,
            remove: false,
            useDisplay: false
        });
    });
    
    Roo.select('.body-area-right-product-image img', true).on('click', function() {
       // lightbox image..
        if (!lightbox) {
            lightbox = new Roo.ux.Lightbox({
                imageArray :  Roo.select('.body-area-right-thumbs img',true).elements
            });    
        }
        //decide which one to start on..
       
        
        var match = this.dom.src.replace( 
            new RegExp( '_' + img_scales[1] + '\.png$'), 
            '_' +  img_scales[1]+ '.png');
        var startid = false;
        Roo.each(lightbox_image_div.query('img'), function(img) {
            if (img.src == match) {
                startid = Roo.get(img);
                return true;
            }
        });
        
        
        lightbox.start(startid);
    });
    
    Roo.select('#pulldown-colour').on('change', function() {
       // Roo.log(this.value);
        colSelect(this.value, 0, this.value);
    });
    
    
    if (!Roo.select('.body-area-right-thumbs img',true)) {
        return;
    }
    if (!image_css) {
        
        image_css = Roo.util.CSS.createStyleSheet( {
            '.bg-loading' : { 
                background:'url('+ rootURL  + '/roojs1/images/ux/lightbox/loading.gif) 0 0 no-repeat', 
                display:'block',
                position: 'absolute',
                'z-index' : 10000,
                width:'32px',
                height:'32px'
            }
        });
        
        image_mask = Roo.DomHelper.append(document.body, {
            cls: 'bg-loading',
            display: 'block'
        },true);
        image_mask.setVisibilityMode(Roo.Element.DISPLAY);
        image_mask.hide();
         
    };
    // thumbs..
    Roo.select('.body-area-right-thumbs img',true).each(function(img) {
        
        
        img.href = img.dom.src.replace( 
            new RegExp( '_' + img_scales[0] + '\.png$'), 
            '_' +  img_scales[2]+ '.png'); 
        
        img.on('click', function() {
            var s2 = Roo.get(this.dom.parentNode).hasClass('img-tall') ? 
                img_scales[3] : 
                img_scales[1];
            
            var newurl = this.dom.src.replace( new RegExp( '_' + img_scales[0] + '\.png$'), 
            '_' +  s2 + '.png');
          
            
            showImage(newurl,
                // wher to show
                Roo.get(this.dom.parentNode).hasClass('img-tall') ?
                Roo.select('.body-area-right-product-image img', true).elements[1]  : pimage);
         
        })
    });
    if (Roo.select('.body-area-right-thumbs img').elements.length < 2) {
        Roo.select('.body-area-right-thumbs').elements[0].style.display = 'none';
    }
      

    
}

function showImage(newurl, target) {
    //Roo.log(newurl);
    target = target || pimage;
    var xy = image_mask.getAlignToXY(target,'c');
    target.fadeOut({
        endOpacity: 0, //can be any value between 0 and 1 (e.g. .5)
        easing: 'easeOut',
        duration: .2,
        remove: false,
        useDisplay: false, 
        callback : function() {
            image_mask.show();
            image_mask.setXY(xy);
            
            
            target.dom.src = newurl;
            // onload should handle fade..
            
            
           
        }
    });
}

var productPrice = 0;
var productOldPrice = 0;

function fnSubmit(targ)
{
	document.getElementById(targ).submit();
}

function fnSubmitCountry()
{
	if (document.forms['internationalSitesForm'].internationalSite.value != "null")
    {
		document.location.href = document.forms['internationalSitesForm'].internationalSite.value;
	}
}

function fnDisable(targ)
{	
	document.getElementById(targ).style.position = "absolute";
	document.getElementById(targ).style.top = "-10000px";
	document.getElementById(targ).style.position = "absolute";
}

function fnRemoveDefaultText(targ)
{
	document.getElementById(targ).value = "";
}
/*
function fnMenu()
{
	var oMenu = document.getElementById("menu");
	var oMenuItems= oMenu.getElementsByTagName("A");
	for (var i=0; i<oMenuItems.length; i++)
    {
		if (oMenuItems[i].className != "selected") {
            oMenuItems[i].onmouseover = function() {
                    this.firstChild.src = images[this.id][1];
                }
            oMenuItems[i].onmouseout = function() {
                    this.firstChild.src = images[this.id][0];
            }
		} else
        {
			oMenuItems[i].firstChild.src = images[this.id][1];
        }
	}	
}
*/
function fnConfig()
{
	// fnMenu();
}
 
function fnCloseChromeless()
{
	if (document.getElementById("flashH3"))
    {
		document.getElementById("flashH3").style.visibility = "visible";
	}
	var oPopUp = document.getElementById("chromelessPopUp");
	document.body.removeChild(oPopUp);
}

function fnPViewTarget(name,img)
{
	var targ = document.getElementById("magnifyLink");
	targ.href = "javascript:fnEnlarge('enlargeImage.aspx?name="+name+"&i="+img+"')";
}

function fnColourSelect(targ)
{ 
	var oToolsItems = document.getElementById("toolbar").getElementsByTagName("A");		
	for (i=0; i<oToolsItems.length; i++)
    {
		oToolsItems[i].className = "deselected";
	}
	document.getElementById(targ).className = "selected"
	document.getElementById("colourName").innerHTML = document.getElementById(targ).firstChild.alt;
}

/* View extra info */
/*function fnInfo(state) {
	if(document.getElementById("info")) {
		document.getElementById("info").style.display = state;
		document.getElementById("blockLink").style.display = "block";
		document.getElementById("noneLink").style.display = "block";
		document.getElementById(state+"Link").style.display = "none";	
	}		
}*/

function fnShow(targ)
{
	document.getElementById(targ).style.display = "block";
}

function fnHide(targ)
{
	document.getElementById(targ).style.display = "none";
}

/* Checkout functions  -- not used!?*/
function fnRadio(id,display)
{
	document.getElementById(id).style.display = display;
}

function fnChecked(id)
{
	document.getElementById(id).checked = "checked";
}

/**/ 


/** ---------------- HANLDING BASKET -----------------*/





function selectItem()    
{
    var selectedValues = []; 
    var inputs = document.getElementsByTagName('select');
    for (var i = 0; i < inputs.length; i++){
	
        var ind = searchFields.indexOf(inputs[i].name);
        if (ind != -1){
            selectedValues[ind] = inputs[i].value;
        }
    }
    // selectedValues contains list of values.
    function compareItem(pi) {
        var match =true;
        for (var i =0 ; i < searchFields.length; i++) {
            if (pi[searchFields[i]] != selectedValues[i]) {
                
                return false;
            }
        }
        return true;
        
    }
    
    // if isset items with this values
    var inStock = false;
    var add="";
    for (var i = 0; i < itemsArray.length; i++){
        if (!compareItem(itemsArray[i])) {
            continue;
        }
         
        inStock = true;
        if ((itemsArray[i].visible * 1) != 1) {
            inStock = false;
            
        }
        
        
        add = itemsArray[i]['product_id'] + '.' + itemsArray[i]['id'];
        productPrice = itemsArray[i]['price'];
        productOldPrice = Number(itemsArray[i]['base_price']) > Number(itemsArray[i]['price']) ? itemsArray[i]['base_price'] : 0 ;
        
        break;
        
    }
    if (searchFields.length != 0) {
        document.getElementById('growb').add.value = add;
    }
    if (inStock) {
        var itemsCount = 15;
        showQuantity(itemsCount);
    } else {
        showNotInStock();
    }
    setTotalPrice();
}
// GLOBAL ! productPrice 
function setTotalPrice()
{
    var total = 0;
    var quantitySelect = document.getElementById("productQty");
    if (quantitySelect.style.display == 'none'){
        oldtotal = 0;
        total = 0;
    } else {

        var price = Number(productPrice);
        total = (price * Number(quantitySelect.value)) ;
        oldtotal = (Number(productOldPrice) * Number(quantitySelect.value)); 
    }
    var span = document.getElementById('subTotalValue');

    span.innerHTML = (oldtotal ? ("<s>US$" + oldtotal.toFixed(2)+ "</s> ") : "") + "US$" + total.toFixed(2);
}

/**
 * show not in stock part of page
 */
function showNotInStock()
{
    
    document.getElementById("quantity").style.display = 'none';
    document.getElementById("subTotal").style.display = 'none';
    document.getElementById("stockOut").style.display = 'block';
    document.getElementById("addBasketItem").disabled = true;
    document.getElementById("addBasketItem").style.display = 'none';
    // todo
}

function colSelect(txtname, image_id, col) {
    
    
    if (!image_id) {
        for(var i =0;i < itemsArray.length; i++) {
            if (itemsArray[i].colour == col && itemsArray[i].image_id * 1) {
                //Roo.log(itemsArray[i]);
                image_id = itemsArray[i].image_id;
                break;
            }
        }
    }
    if (!image_id) {
        return;
    }
    
    document.getElementById('colourName').innerHTML=txtname;
    
    //buildEnlargeLink(image_id);
    
    Roo.select('.body-area-right-thumbs img',true).each(function(img) {
        
        
        
        
        if (img.dom.src.split('/').pop().split('_').shift() == image_id) {
            var newurl = img.dom.src.replace( 
                new RegExp( '_' + img_scales[0] + '\.png$'), 
                '_' +  img_scales[1]+ '.png');
            
            showImage(newurl);
        }
    });
    
    
    document.getElementById('pulldown-colour').value=col;
    selectItem();
}
/**
 * show quantity selectbox
 *
 *@param itemsCount - count of items in stock
 */
function showQuantity(itemsCount)
{
    var quantitySelect = document.getElementById("productQty");
    if (!quantitySelect.childNodes.length) {
        for (var i = 0; i < itemsCount; i++){
            var e = document.createElement('option');
            e.setAttribute('value', i+1);
            e.setAttribute('selected', i == 0 ? 'selected' : '');
            e.innerHTML  = i+1;
            quantitySelect.appendChild(e);
        }
        quantitySelect.selectedIndex=0;
    }
      
    document.getElementById("subTotal").style.display = 'block';
    document.getElementById("quantity").style.display = 'block';
    document.getElementById("stockOut").style.display = 'none';
    document.getElementById("addBasketItem").disabled = false;
    document.getElementById("addBasketItem").style.display = 'inline';
    //TODO::hide element with no items property 
}




/* checkout */
function validateInput()
{
    var fields = new Array('email', 'firstname', 'surname', 'street', 'city', 'country', 'phone');
    var errors = 0;
    for (var i = 0; i < fields.length; i++)  {
        var el = document.getElementById(fields[i]+'Label');
        if (!document.getElementById(fields[i]).value) {
            errors ++;
            el.style.color = 'red';
        } else  {
            el.style.color = '';
        }
    }
    if (errors == 0) {
        if (!document.getElementById('terms').checked)
        {
            alert('You must accept out Terms and Conditions for continue!');
            return false;
        }
        return true;
    } else  {
        document.getElementById('errorDiv').style.display = 'block';
        return false;
    }
}

function searchKeyUp(myfield,e) {
    
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;
    console.log(keycode);
    if (this.value.length) {
        return;
       }
    if (keycode == 13) {
        this.form.submit();
        return false;
    }

    return true;

}

