//pxl.mhs.brochureshopform.js
//author: 			Bjoern Sellnau @ PIXELTEX
//project:			Main JS for the MHS Brochureshop
//description:	this is the main file of the mhs brochureshop
//added:				02-09-2009
//modified:			09-09-2009
//version				0.15
//=====================================================

//*** NAMESPACES ***
	if(!pxl) var pxl={};
	if(!pxl.mhs) pxl.mhs={};
//*** VARIABLES ***
pxl.mhs._items = new Array();

/* get all details of the given item */
pxl.mhs.getItemDetails = function(id)
{
	pxl.mhs.playLoadingAnimation();
	var url = 'mhs_shoppingcart.cfm';
	$.get( url, {action:'getItemDetail',agent_id:AGENT_ID,item_id:id},
	function(data)
	{ 
		var DATA = JSON.parse(data);
		pxl.mhs.stopLoadingAnimation();
		pxl.mhs._currentBrochure = DATA.ITEM_ID;

		//window.console.log(DATA);		

		//set title
		$('.broschuereHL').html('<h2>'+DATA.ITEMNAME+'</h2>');		

		//set description
		$('.broschuereText').html(DATA.TEASER+'<br/><br/>'+SEASON_TEXT+': '+DATA.SEASON);		

		//set image
		$('#contentImage').attr('src',MHS_URL+DATA.IMAGE);		
		
		//set id at 'add to cart link'
		$('.mhs_addto_cart').attr('rel',DATA.ITEM_ID);
		
		//set id at 'download'
		$('.mhs_download_link').attr('href',MHS_URL+'download.cfm?item='+DATA.ITEM_ID);

		//set id at 'browser brochure link'
		$('.mhs_browse_link').attr('rel',DATA.ITEM_ID);
		
		if(pxl.mhs._itemExists(DATA.ITEM_ID) || pxl.mhs._getItemsNum() >= 5)
		{
			$('.mhs_addto_cart').addClass('wklink_full').attr('rel','');
		} else {
			$('.mhs_addto_cart').removeClass('wklink_full');		
		}

	})
}

pxl.IsThisBrowserIE6 = function() 
{
    return ((window.XMLHttpRequest == undefined) && (ActiveXObject != undefined))
}

/* play loading animation */
pxl.mhs.playLoadingAnimation = function()
{
	$('#contentImage').css('opacity','0.5');
	$('#loaderOverlay').css('display','block');	
}

/* stop loading animation */
pxl.mhs.stopLoadingAnimation = function()
{
	$('#contentImage').css('opacity','1');
	$('#loaderOverlay').css('display','none');	
}

/* this removes an item from the shoppingcart */
pxl.mhs.removeItemFromCart = function(ID,exclude)
{

		var url = 'mhs_shoppingcart.cfm';
		var RANDOM = new Date().getTime()/1000;

	    if(pxl.IsThisBrowserIE6() == false)
	    {
		    $('#wkOverlay'+ID+' img').attr('src','img/ajax_loader3.gif').css({visibility:'visible'}).parent().next().css('opacity','0.5');
	    }
	    if(pxl.IsThisBrowserIE6())
	    {
    		$('#wkOverlay'+ID+' img').attr('src','img/ajax_loader3.gif');
	        $('#wkOverlay'+ID).css('background-image','url(img/ie6_cover.png)').get(0).style.filter = 'Alpha(opacity=70)';
	    }		

        $.get(url, {action:'removeItem',id:ID,random:RANDOM},
		function(data)
		{ 
/*
		var beforeRemove = pxl.mhs._getItemsNum();
		pxl.mhs._removeItem(ID);
		var afterRemove = pxl.mhs._getItemsNum();
		
		if(afterRemove > 0)
		{
			$('div.wkNumber img').attr('src','img/layout/wk0'+afterRemove+'.jpg');
		} else {
			$('.wkBestellen').css('visibility','hidden');
		}
*/

		$('#wkItem'+ID).fadeOut('fast',function()
		{
			$(this).remove();

    		if($('.wkOverlay').length < 1)
    		{
    		    $('.submitFormLink').css('visibility','hidden');
    		} else {
    		    $('.submitFormLink').css('visibility','visible');		    
    		}
		})
/*
		if(pxl.mhs._getItemsNum() >= 5 || pxl.mhs._itemExists(pxl.mhs._currentBrochure))
		{
			$('.mhs_addto_cart').addClass('wklink_full').attr('rel','');
		} else {
			$('.mhs_addto_cart').removeClass('wklink_full').attr('rel',ID);		
		}
*/
			//var DATA = JSON.parse(data);


		})
		
		

	/*
		1) check if an item with teh given ID exists if yes remove item from _items array.
		2) update grafik of shoppingcart and remove little icon from 'div.warenkorb'
		3) call the iframe: mhs_shooppingcart.cfm?action=removeItem&ID=...
		4) test if less than 5 items are in the cart if yes 'activate the add to cart link'
		
	*/
}

/* this removes an item from the _items array */
pxl.mhs._removeItem =function(ID)
{
	for(var i=0;i<=pxl.mhs._items.length-1;i++)
	{
		if(pxl.mhs._items[i].id == ID)
		{
			pxl.mhs._items.splice(i,1);
		}
	}
}

/* this removes all items from the _items array */
pxl.mhs._removeAll =function(ID)
{
}

/* this gets the length of the _items array */
pxl.mhs._getItemsNum =function()
{
	return pxl.mhs._items.length;
}

/* this adds an item to the _items array */
pxl.mhs._itemExists =function(item)
{
	for(var i=0;i<=pxl.mhs._items.length-1;i++)
	{
		if(pxl.mhs._items[i].id == item)
		{
			return true;
		}
	}
}
