/**
 *
 * TechDivision_RitterSportTheme
 *
 * NOTICE OF LICENSE
 *
 * TechDivision_RitterSportTheme is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TechDivision_RitterSportTheme is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TechDivision_RitterSportTheme. If not, see <http://www.gnu.org/licenses/>.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade TechDivision_RitterSportTheme to newer
 * versions in the future. If you wish to customize TechDivision_RitterSportTheme for your
 * needs please refer to http://www.techdivision.com for more information.
 *
 * @category   	TechDivision
 * @package    	TechDivision_RitterSportTheme
 * @copyright  	Copyright (c) 2011 <info@techdivision.com> TechDivision GmbH
 * @license    	<http://www.gnu.org/licenses/>
 *			  	GNU General Public License (GPL 3)
 * @author      Thomas Kalteis <tk@techdivision.com>
 **/
/**
 * js/global.js
 *
 * Global JS functionality used in the Theme
 *
 * @category   	TechDivision
 * @package    	TechDivision_RitterSportTheme
 * @copyright  	Copyright (c) 2011 <info@techdivision.com> TechDivision GmbH
 * @license    	<http://www.gnu.org/licenses/>
 *			  	GNU General Public License (GPL 3)
 * @author      Thomas Kalteis <tk@techdivision.com>
  */

//FadeOut Fix for jQuery
jQuery.fn.fFix = function() {
    return jQuery(this).css("opacity","");
};

/*
 * Live-Code
 */ 

jQuery(document).ready(function(){

	// Display top navigation
	jQuery("li.nav-point-1").hover(
		function () {
			//jQuery("h1#headline,div#customer-name").stop().hide();		
			jQuery("ul#nav-top").css("width","auto");
			
			setTimeout(function(){
				jQuery("li.nav-point-3").fadeIn(175,function(){jQuery(this).fFix();});
			},0);
			
			setTimeout(function(){
				jQuery("li.nav-point-4").fadeIn(175,function(){jQuery(this).fFix();});
			},60);
			
			setTimeout(function(){
				jQuery("li.nav-point-5").fadeIn(175,function(){jQuery(this).fFix();});
			},120);
			
			setTimeout(function(){
				jQuery("li.nav-point-6").fadeIn(175,function(){jQuery(this).fFix();});
			},180);
		}, 
		function () {
		}
	);
	
	// Hide top navigation
	jQuery("ul#nav-top").mouseleave(
		function () {
			setTimeout(function(){
				jQuery("li.nav-point-3").fadeOut(175,function(){jQuery(this).fFix();});
			},180);
			
			setTimeout(function(){
				jQuery("li.nav-point-4").fadeOut(175,function(){jQuery(this).fFix();});
			},120);
			
			setTimeout(function(){
				jQuery("li.nav-point-5").fadeOut(175,function(){jQuery(this).fFix();});
			},60);
			
			setTimeout(function(){
				jQuery("li.nav-point-6").fadeOut(175,function(){jQuery(this).fFix();});
			},0);	
		}
	);
	
	// Show temperature layer 
	jQuery("div#temperature").hover(
		function () {
                    (jQuery.browser.msie) ? jQuery("div#shoppingcart-layer").stop().hide() : jQuery("div#shoppingcart-layer").stop().fadeOut(175,function(){jQuery(this).fFix();});
                    (jQuery.browser.msie) ? jQuery("div#temperature-layer").show() : jQuery("div#temperature-layer").fadeIn(175,function(){jQuery(this).fFix();});
		}, 
		function () {
                   (jQuery.browser.msie) ? jQuery("div#temperature-layer").hide() : jQuery("div#temperature-layer").fadeOut(175,function(){jQuery(this).fFix();});
		}
	);
	
	// Show shoppingcart layer
	jQuery("div#shoppingcart").hover(
		function () {
                    (jQuery.browser.msie) ? jQuery("div#temperature-layer").stop().hide() : jQuery("div#temperature-layer").stop().fadeOut(175,function(){jQuery(this).fFix();});
                    (jQuery.browser.msie) ? jQuery("div#shoppingcart-layer").show() : jQuery("div#shoppingcart-layer").fadeIn(175,function(){jQuery(this).fFix();});
		}, 
		function () {
                    (jQuery.browser.msie) ? jQuery("div#shoppingcart-layer").hide() : jQuery("div#shoppingcart-layer").fadeOut(175,function(){jQuery(this).fFix();});
		}
	);
	
	// Hover teaser	
	jQuery("div#teaser div.col-1, div#teaser div.col-2, div#teaser div.col-3, div#teaser div.col-4").hover(
		function () {
			jQuery(this).addClass('active');
		}, 
		function () {
			jQuery(this).removeClass('active');
		}
	);
	
	// Show custom product packing overlay (PIV)
	jQuery('a[rel=piv-overlay]').live('click',function() {
		jQuery(this).Overlay('piv', this.href);
		return false;
	});
	
	jQuery('a.piv-fancybox').fancybox({
		'hideOnContentClick': false,
		'modal': true,
		'titleShow': false,
		'overlayColor': '#333'
	});
	
	jQuery('a.rsfp-fancybox').fancybox({
		'hideOnContentClick': false,
		'hideOnOverlayClick' : false,
		'titleShow': false,
		'overlayColor': '#333',
		'onComplete': function() {
			blockFooter();
		}
	});
	// fancybox hyperlinks product info
	jQuery('a.fancybox').fancybox({
		'titlePosition': 'inside',
		'titleFormat': productInfoTitle,
		width: 900,
		height: 410
	});
	
	//Mini-Login-Form Submit on Enter
	jQuery('#toolbox form.login input').keydown(function(e){
        if (e.keyCode == 13) {
            jQuery(this).parents('form').submit();
            return false;
        }
    });
	
    //Mini-Login-Form Submit on Click a-Tag
    jQuery('#toolbox form.login a').click(function(){
        jQuery(this).parents('form').submit();
		return false;
    });
    



	/*
 * Simple - Class
 */
Simple = function(options) {
	this.settings = jQuery.extend({
        basePrice: 0.0,
        digits:2
	},options);

		this.shoppingcart = jQuery('table.product-page-shoppingcart');
    this.rows = this.shoppingcart.find('tr:not(.total)');
    this.gespricelbl = this.shoppingcart.find('tr.total span.product-price');
    this.gescounter = this.shoppingcart.find('tr.total input.product-quantity');
    this.submitbtn = this.shoppingcart.find('tr.total input.product-submit');
		// TD Tested until here
};
var SI = Simple.prototype;
//Get Totals
SI.getTotals = function() {
    var totalprice = 0, totalcount = 0, t = this;
    //Get totals
    t.rows.each(function(){
        var r = jQuery(this), counter = r.find('input.product-quantity'), price = parseFloat(r.find('span.product-price').text().replace('�','').replace('&euro;','').replace(' ','').replace('&nbsp;','').replace(',','.')) || 0;
        var count = counter.val() ? parseInt(counter.val()) : 0;
        totalcount+=count;
        totalprice+=price*count;
    });
//		alert(totalcount);
//		alert(totalprice);
    //Write totals
    t.gescounter.val(totalcount);
    t.gespricelbl.html(tFix(totalprice, t.settings.digits)+'&nbsp;&euro;');
};

//Draw Simple Product
SI.draw = function() {
    var t = this, s = t.settings;
    t.rows.each(function(){
        var r = jQuery(this), decinc = r.find('input.dec, input.inc'), counter = r.find('input.product-quantity');
       alert('rtes');
        //De-/Increment Event 
        decinc.click(function(){
    	    var count = parseInt(counter.val());
            (jQuery(this).hasClass('inc')) && counter.val(count+1);
            (jQuery(this).hasClass('dec')) && (count > 0) && counter.val(count-1);
            //calculate result count
            count = parseInt(counter.val());
            t.getTotals();
            return false;
        });
        //Catch ENTER
        counter.keydown(function(e) {
            if(e.keyCode == 13) { t.submitbtn.click(); return false; }
        });
        //Counter Key-Event
        counter.keyup(function(){
            t.getTotals();
            return false;
        }); 
        //Make sure that Count in Counter >= 0
        counter.blur(function() {
            var count = parseInt(counter.val());
            !(count >= 0) && counter.val('0');
            counter.keyup();
        });
    });
};
	// Author: Edoras: Manuel Richarz
	//Slider überarbeitet by TD using coverflip and jQuery UI
	if (jQuery('#flip').length > 0)
	{
		jQuery('#flip').jcoverflip({
		    change: function(event, ui){
		        jQuery('#scrollbar').slider('value', ui.to);
		    },
		    beforeCss: function( el, container, offset ){
		    	el.removeClass('active');
		    	el.children('.product-image').unbind('mouseenter mouseleave');
			    return [
			      jQuery.jcoverflip.animationElement( el, { left: ( container.width( )/2 - 220 - 177*offset - 65)+'px', top: '80px' }, { } ),
			      jQuery.jcoverflip.animationElement( el.find( 'img' ), { width: '167px' }, {} )
			    ];
			},
			afterCss: function( el, container, offset ){
				el.removeClass('active');
		    	el.children('.product-image').unbind('mouseenter mouseleave');
			    return [
			      jQuery.jcoverflip.animationElement( el, { left: ( container.width( )/2 + 177*offset + 118)+'px', top: '80px' }, { } ),
			      jQuery.jcoverflip.animationElement( el.find( 'img' ), { width: '167px' }, {} )
			    ];
			},
			currentCss: function( el, container ){
	    		el.addClass('active');
				el.children('.product-image').hover(
						function () {
							
							var position = jQuery(this).children('.img-product').position();
							var topPosition = 15 + position.top + jQuery(this).children('.img-product').height() / 2 - jQuery(this).children('.tooltip-wrapper').height() / 2;
							var marginLeft = jQuery(this).children('.img-product').width() / 2;
							
							//fix tooltip position
							if (jQuery(this).children('.tooltip-wrapper.bottom').length > 0)
							{
								jQuery(this).children('.tooltip-wrapper').css({'top' : (position.top + 100 - jQuery(this).children('.tooltip-wrapper').height()) + 'px', 'margin-left' : marginLeft + 'px'});						
							}
							else if (jQuery(this).children('.tooltip-wrapper.top').length > 0)
							{
								jQuery(this).children('.tooltip-wrapper').css({'top' : position.top + 'px', 'margin-left' : marginLeft + 'px'});								
							}
							else
							{
								jQuery(this).children('.tooltip-wrapper').css({'top' : topPosition + 'px', 'margin-left' : marginLeft + 'px'});
							}
							(jQuery.browser.msie) ? jQuery(this).children('.tooltip-wrapper').show() : jQuery(this).children('.tooltip-wrapper').fadeIn(175,function(){jQuery(this).fFix();});
						}, 
						function () {
						    (jQuery.browser.msie) ? jQuery(this).children('.tooltip-wrapper').hide() : jQuery(this).children('.tooltip-wrapper').fadeOut(175,function(){jQuery(this).fFix();});
						});
			    return [
			      jQuery.jcoverflip.animationElement( el, { left: ( container.width( ) / 2 - 105 )+'px', top: '55px' }, { } ),
			      jQuery.jcoverflip.animationElement( el.find( 'img' ), { width: '210px' }, { } )
			    ];
			}
		  });
		  
		jQuery('#scrollbar').slider({
		    value: 0,
		    max: jQuery('#flip').jcoverflip('length') - 1,
		    slide: function(event, ui) {
		      if(event.originalEvent) {
		        var newVal = Math.round(ui.value);
		        jQuery('#flip').jcoverflip('current', newVal);
		        jQuery('#scrollbar').slider('value', newVal);
		      }
		    }
		});
		
		jQuery('.leftscroll').click(function(){
			jQuery('#flip').jcoverflip('previous');
		});
		
		jQuery('.rightscroll').click(function(){
			jQuery('#flip').jcoverflip('next');
		});
	}
});

// create an Overlay with Ajax-Content (used by PresentBundle and PIV)
jQuery.fn.Overlay = function(id, url, iframe) {
	
    var t = jQuery(this);
    id = id || t.attr('rel') || t.attr('id');
    jQuery('body').append('<div id="'+id+'"></div>');
	var o = jQuery('#'+id);
	jQuery('div#overlay-wrapper-bg, div#overlay-wrapper.'+id).fadeIn(175);
	jQuery('div#overlay-wrapper-bg').css({"opacity":"0.5"});
	
	// write iFrame
	o.append('<div id="overlay-wrapper" class="'+id+'">'+
        '<div id="overlay-wrapper-content">'+
        '<iframe style="width:870px; height:334px; overflow:hidden; background-color: #FFFFFF;" id="iframe-'+id+'" src="'+url+'"></iframe>'+
        '<a title="Schließen" href="#" id="lbClose"></a>'+
        '<div>cart goes here...</div>' +
        '</div>'+
        '</div>');
	
    o.find('a#lbClose').click(function(){
        jQuery(this).parents('div#'+id).fadeOut(175, function(){ jQuery(this).remove(); });
	    jQuery('div#overlay-wrapper-bg').fadeOut(175);
	    return false;
    });
	//Write Ajax-Content
    /*
	o.load(url, function(){
	    o.find(':jsclass').generateObject();
	    o.find('a#close-overlay').click(function(){
	        jQuery(this).parents('div#'+id).fadeOut(175, function(){ jQuery(this).remove(); });
		    jQuery('div#overlay-wrapper-bg').fadeOut(175);
		    return false;
	    });
    });
	*/
};

/**
 * Renders the markup for the fancybox title area in the
 * further infos view.
 */
function productInfoTitle(title) {
	return '<ul id="nav-overlay"><li class="last"><span>' + title + '</span></li></ul>';
}
