
// Hint hovers
$$('.hint').each(function(el) {
    el.onmouseover = function() {$(this.readAttribute('rel')).style.display = 'block';}
    el.onmouseout = function() {$(this.readAttribute('rel')).style.display = 'none';}
});

if ($$('#bestdeal-hoverarea').length > 0) {
    $('bestdeal-hoverarea').observe('mouseover', function(event) {
	$('bestdeal-container').show();
	$('bestdeal-container').style.display = 'block';
    });
    $('bestdeal-hoverarea').observe('mouseout', function(event) {
	$('bestdeal-container').hide();
	$('bestdeal-container').style.display = 'none';
    });
}

if ($$('#bestdeal-container').length > 0) {
    $('bestdeal-container').observe('mouseover', function(event) {
	$('bestdeal-container').show();
	$('bestdeal-container').style.display = 'block';
    });
    $('bestdeal-container').observe('mouseout', function(event) {
	$('bestdeal-container').hide();
	$('bestdeal-container').style.display = 'none';
    });
}



/*
// Category hover
$$('#bestdeal').each(function(el) {
//    el.onmouseover = function() {$$('#bestdeal .nav-container')[0].style.display = 'block'; };
//    el.onmouseout  = function() {$$('#bestdeal .nav-container')[0].style.display = 'none'; };
//    el.observe('mouseover', function() { $$('#bestdeal .nav-container')[0].style.display = 'block';  });
//    el.observe('mouseout', function() {$$('#bestdeal .nav-container')[0].style.display = 'none'; });
//
    Event.observe( el, 'mouseover', function(event) {
            $$('#bestdeal #bestdeal-container')[0].style.display = 'block';
//            $$('#bestdeal #bestdeal-container')[0].style.border = '2px solid blue';
    });
//    Event.observe( el, 'mouseout', function(event) {
//        if (el == Event.findElement( event )) {
//            $$('#bestdeal #bestdeal-container')[0].style.display = 'none';
//            $$('#bestdeal #bestdeal-container')[0].style.border = '2px solid green';
//        }
//    });
    Event.observe( $$('#bestdeal #bestdeal-container')[0], 'mouseout', function(event) {
//        if ($$('#bestdeal #bestdeal-container')[0] == Event.findElement( event )) {
            $$('#bestdeal #bestdeal-container')[0].style.display = 'none';
//            $$('#bestdeal #bestdeal-container')[0].style.border = '2px solid red';
//        }
    });
});
*/

// Faqslider click
if ($$('#faqslider').length > 0) {
    Event.observe('faqslider', 'click', function(event) {
        if ($('faqboxsub').style.display == '' || $('faqboxsub').style.display == 'none') {
            $('faqboxsub').style.display = 'block';
        } else {
            $('faqboxsub').style.display = 'none';
        }
    });
}

// Product progressbar width grow
if ($$('#dealprogress-slider').length > 0) {
    $('dealprogress-slider').writeAttribute('curWidth', 0);
    progressWidth = window.setInterval(function() {

        $('dealprogress-slider').writeAttribute('curWidth', parseInt($('dealprogress-slider').readAttribute('curWidth'))+1 );
        $('dealprogress-slider').style.width = parseInt($('dealprogress-slider').readAttribute('curWidth'))+'%';
        if (parseInt($('dealprogress-slider').readAttribute('curWidth')) >= parseInt( $('dealprogress-slider').readAttribute('rel') )) {
            clearInterval( progressWidth );
        }
    }, 50); // 100 is the speed, increase this for a slower growth
}

if ($$('#howitworks-link').length > 0) {
    Event.observe('howitworks-link', 'click', function(event) {
        $$('#howitworksdata ul.ui-tabs-nav a').each(function(sube) {
	
	    var elclass = (new String(sube.up().readAttribute('class')) == 'null' ? sube.up().getAttribute('class') : sube.up().readAttribute('class') );
	
	    sube.removeClassName('active');
	    $$('#howitworksdata #'+elclass+'Tab')[0].hide();
        });

	if ($$('#howitworksdata ul.ui-tabs-nav a:first').length > 0) {
	    $$('#howitworksdata ul.ui-tabs-nav a:first')[0].addClassName('active');
	}
	
	if ($$('#howitworksdata div.ui-tabs-panel:first').length > 0) {
	    $$('#howitworksdata div.ui-tabs-panel:first')[0].show();
	}

        if ($('howitworksdata').style.display != 'block') {
            $('howitworksdata').style.display = 'block';
        } else {
            $('howitworksdata').style.display = 'none';
        }

        return false;
    });

    $$('#howitworksdata ul.ui-tabs-nav a').each(function(e) {
        e.up().removeClassName('ui-corner-top');
        Event.observe(e, 'mouseover', function(event) {
            $$('#howitworksdata ul.ui-tabs-nav a').each(function(sube) {
		var subelclass = (new String(sube.up().readAttribute('class')) == 'null' ? sube.up().getAttribute('class') : sube.up().readAttribute('class') );
                sube.removeClassName('active');
                $$('#howitworksdata #'+subelclass+'Tab')[0].hide();
            });
	    
	    var elclass = (new String(e.up().readAttribute('class')) == 'null' ? e.up().getAttribute('class') : e.up().readAttribute('class') );
            $$('#howitworksdata #'+elclass+'Tab')[0].show();
            e.addClassName('active');
        });
    });

    $$('#howitworksdata a.closeworks').each(function(e) {
        Event.observe(e, 'click', function(event) {
            $('howitworksdata').style.display = 'none';
            return false;
        });
    });
}

function productShowExtendedOptions() {
    ops = $$('#gd-popup .option-container');
    for(i = 0; i < ops.length; i++) {
        ops[i].addClassName('rounded');
    }

    els = $$('#gd-popup .product_options');
    for(i = 0; i < els.length; i++) {
        lists = els[i].getElementsByTagName('dl');
        sels = lists[0].getElementsByTagName('select');
	if (sels.length > 0) {
	    sels[0].selectedIndex = i+1;
	    lists[0].style.display = 'none';
	}
    }

    $$('#gd-popup #extendedoptions input, #gd-popup #extendedoptions select').each( function( el ) {
	Event.observe(el , 'change', function() { reloadGDPrice(); });
    });
    
    reloadGDPrice();
}

function reloadGDPrice() {
    

    $$('#gd-popup form').each(function( frmEl ) {
	
	priceElement = frmEl.select('button .price');
	price = parseFloat( priceElement[0].readAttribute('rel'));
	
	if (new String(price) == 'null') { price = parseFloat( priceElement[0].getAttribute('rel')); }
	
	frmEl.select('input').each(function( el ) {
	    ElName = ( new String(el.readAttribute('name')) == 'null' ? el.getAttribute('name') : el.readAttribute('name') );
	    if (/options/.test( ElName )) {
		optId = ElName.replace(/options\[(\d+)\]/, '$1');
		optPrice = opConfig.config[ optId ];
		
		if (el.hasClassName('required-entry') || el.value != '') {
		    price = (parseFloat(price)+parseFloat(optPrice));
		}
	    }    
	});
	
	frmEl.select('select').each(function( el ) {
	    ElName = ( new String(el.readAttribute('name')) == 'null' ? el.getAttribute('name') : el.readAttribute('name') );
	    if (/options/.test( ElName )) {
		optId = ElName.replace(/options\[(\d+)\]/, '$1');
		optPrice = opConfig.config[ optId ];

		if ((el.hasClassName('required-entry') && el.value != '' ) || el.value != '') {
		    price = (parseFloat(price)+parseFloat( optPrice[el.value] ));
		}
	    }
	});
	
	priceElement[0].innerHTML =  new String(parseFloat(price).toFixed(2)).replace('.', ',');
    });
}

function productValidateOptions( el ) {

   var elRel = 'gd-'+(new String(el.getAttribute('rel')) != 'null' ? el.getAttribute('rel') : el.readAttribute('rel') );

   $(el).up('form').writeAttribute('id', elRel);

   var formvalid = new VarienForm( elRel );

    if (formvalid.validator.validate()) {
        $( elRel ).submit();
    }
}
function popupOverlay( elmName, callback ) {

	bod = document.getElementsByTagName('body')[0];

    if (document.getElementById('gd-overlay') == null) {
        overlay = document.createElement('div');
    	overlay.id = 'gd-overlay';
        overlay.onclick = function() {popupClose();}
        bod.appendChild(overlay);
    }

    if (document.getElementById('gd-popup') == null) {
        an = document.createElement('div');
    	an.id = 'gd-popup';
        bod.appendChild(an);
    } else {
        an = document.getElementById('gd-popup');
    }

    document.getElementById('gd-overlay').style.display = 'block';

	an.innerHTML = '<div id="'+elmName+'">'+document.getElementById( elmName ).cloneNode(true).innerHTML+'</div>';
    an.style.display = 'block';

    if (callback) callback();

    // If we're not using ie 6 or lower and the popup box stretches past the body height, reset the positioning
    if (/MSIE (6|5.5|5.0)/g.test(navigator.userAgent) == false) {
        if ($('gd-popup').getHeight()+$('gd-popup').cumulativeOffset()[1] >= document.viewport.getHeight()) {
            an.style.position = 'absolute';
        }
    }
}
function popupClose() {
    document.getElementById('gd-overlay').style.display = 'none';
    document.getElementById('gd-popup').innerHTML = '';
    document.getElementById('gd-popup').style.display = 'none';
    return false;
}
