$(document).ready(function()
{
	/** start menu **/
	$('a#trigger-account').mouseover(function(){$('#sub-nav, #sub-nav-resources, #sub-nav-help').hide();$('#sub-nav-account').show();});
	$('a#trigger-resources').mouseover(function(){$('#sub-nav, #sub-nav-account, #sub-nav-help').hide();$('#sub-nav-resources').show();});
	$('a#trigger-help').mouseover(function(){$('#sub-nav, #sub-nav-resources, #sub-nav-account').hide();$('#sub-nav-help').show();});
	/** end menu **/

	/** start affiliate **/
	// if we have an affiliate session, load the stylesheet
	if($.cookie('affiliate_id') != null && $.cookie('affiliate_id') != 'undefined' && $.cookie('affiliate_id') != ''){load_affiliate();}
	// set affiliate session if it doesn't exist already
	if($.url.param('affiliate') != '' && $.url.param('affiliate') != $.cookie('affiliate_id')){
		set_affiliate($.url.param('affiliate'));
	}else if($.url.param('aff') != '' && $.url.param('aff') != $.cookie('affiliate_id')){
		set_affiliate($.url.param('aff'));
	}
	function set_affiliate(affiliate_id){
		$.ajax({url:'/affiliate.php?action=set_session&id='+affiliate_id,success:function(data){
				if(data != 'false'){
					$.cookie('affiliate_name',data,{path:'/'});load_affiliate();
					$.cookie('affiliate_id',affiliate_id,{path:'/'});load_affiliate();
					window.location = window.location;
				}
			}
		});
	}
	function load_affiliate(){
		var logo = '/images/aff_logos/'+$.cookie('affiliate_id')+'.png';
		$('head').append('<link rel="stylesheet" type="text/css" href="/css/affiliate.css" />');
		$.ajax({url:logo,type:'HEAD',success:function(){
			/*$('#logo img').attr('src', logo);*/
			$('#header .top h1').html('<img src="'+logo+'" />');
			$('#header .top h2').html('<img src="'+logo+'" />');
		}});
		$('#navigation ul li.first a strong').html('Home');
		$('.footer-links').html('Piribo is an affiliate partnership company');
		$('span.search-submit input').attr('src', '/images/btn_search_piribo_grey.gif');
		$('#submitform #promotional_code').parent().after('<p style="padding: 10px 0;">Your transaction will be processed by Piribo Limited on behalf of '+$.cookie('affiliate_name')+'.</p>');
		if ($('#submitform #promotional_code').parent().get(0) != 'undefined') {
			$('.tbl-basket').after('<p style="padding: 10px 0;">Your transaction will be processed by Piribo Limited on behalf of '+$.cookie('affiliate_name')+'.</p>');
		}
	}
	/** end affiliate **/

	/** start product page **/
	$('#item-summary').attr('class','');
	$('#item-contents').attr('class','hide');
	$('#item-related').attr('class','hide');
	$('#item-guarantee').attr('class','hide');
	$('#trigger-item-summary').attr('class','hi');

	$('#trigger-item-summary').click(function(){
		$('#item-summary').attr('class','');
		$('#item-contents').attr('class','hide');
		$('#item-related').attr('class','hide');
		$('#item-guarantee').attr('class','hide');
		$('#trigger-item-summary').parent().addClass('hi');
		$('#trigger-item-contents').parent().removeClass('hi');
		$('#trigger-item-related').parent().removeClass('hi');
		$('#trigger-item-guarantee').parent().removeClass('hi');
		return false;
	});

	$('#trigger-item-contents').click(function(){
		$('#item-summary').attr('class','hide');
		$('#item-contents').attr('class','');
		$('#item-related').attr('class','hide');
		$('#item-guarantee').attr('class','hide');
		$('#trigger-item-summary').parent().removeClass('hi');
		$('#trigger-item-contents').parent().addClass('hi');
		$('#trigger-item-guarantee').parent().removeClass('hi');
		$('#trigger-item-related').parent().removeClass('hi');
		return false;
	});
	
	$('#trigger-item-related').click(function(){
		$('#item-summary').attr('class','hide');
		$('#item-contents').attr('class','hide');
		$('#item-related').attr('class','');
		$('#item-guarantee').attr('class','hide');
		$('#trigger-item-summary').parent().removeClass('hi');
		$('#trigger-item-contents').parent().removeClass('hi');
		$('#trigger-item-related').parent().addClass('hi');
		$('#trigger-item-guarantee').parent().removeClass('hi');
		return false;
	});
	
	$('#trigger-item-guarantee').click(function(){
		$('#item-summary').attr('class','hide');
		$('#item-contents').attr('class','hide');
		$('#item-related').attr('class','hide');
		$('#item-guarantee').attr('class','');
		$('#trigger-item-summary').parent().removeClass('hi');
		$('#trigger-item-contents').parent().removeClass('hi');
		$('#trigger-item-related').parent().removeClass('hi');
		$('#trigger-item-guarantee').parent().addClass('hi');
		return false;
	});
	/** end product page **/

	/** start report feeder generator **/
	$('#frm_rf_generate').click(function(){
		var the_code = '<script type="text/javascript" src="http://www.piribo.com/report_feeder.js?';
		the_code += "k="+$('#frm_search_term').val();
		the_code += "&c="+$('#frm_category_name option:selected').text();
		the_code += "&t="+$('#frm_page_title').val();
		the_code += "&count="+$('#frm_no_of_products').val();
		the_code += "&aff="+$('#frm_affiliate_no').val();
		$('#frm_the_code').val(the_code+'"></script>');
		return false;
	});
	/** end report feeder generator **/

	$('.alerts-toggle').change(function() {
		var is_checked = ($(this).attr('checked'))? true : false;
		$('.alert-categories input[type = "checkbox"]').each(function() {
			if (is_checked) {
				$(this).attr('checked', 'checked');
			} else {
				$(this).attr('checked', '');
			}
		});
	});

	$('a.new-window').click(function(){return !window.open($(this).attr('href'));});
	$('#staged-payment-form').submit(function(){$('#fg-submit_img').attr('disabled','disabled');});
});


function checkUncheckAll (theElement) {
	var theForm = theElement.form, z = 0;
	
	for(z=0; z < theForm.length; z++) {
		if(theForm[z].type == 'checkbox' && theForm[z].name != 'checkall') {
			theForm[z].checked = theElement.checked;
		}
	}
}

function cbutton (formname) {
	document.getElementById('btnCheckout').disabled = true;
	document.getElementById(formname).submit();
}

function more (objId) {
	if(document.getElementById(objId).style.display == 'block') {
		document.getElementById(objId).style.display = 'none';
	} else {
		document.getElementById(objId).style.display = 'block';
	}
}


/*
jQuery Url Plugin
	* Version 1.0
	* 2009-03-22 19:30:05
	* URL: http://ajaxcssblog.com/jquery/url-read-get-variables/
	* Description: jQuery Url Plugin gives the ability to read GET parameters from the actual URL
	* Author: Matthias J�ggli
	* Copyright: Copyright (c) 2009 Matthias J�ggli
	* Licence: dual, MIT/GPLv2
*/
(function($){$.url={};$.extend($.url,{_params:{},init:function(){var paramsRaw="";try{paramsRaw=(document.location.href.split("?",2)[1]||"").split("#")[0].split("&")||[];for(var i=0;i<paramsRaw.length;i++){var single=paramsRaw[i].split("=");if(single[0])this._params[single[0]]=unescape(single[1]);}}catch(e){alert(e);}},param:function(name){return this._params[name]||"";},paramAll:function(){return this._params;}});$.url.init();})(jQuery);

/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

/**
 * Create a cookie with the given name and value and other optional parameters.
 *
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Set the value of a cookie.
 * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
 * @desc Create a cookie with all available options.
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Create a session cookie.
 * @example $.cookie('the_cookie', null);
 * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
 *       used when the cookie was set.
 *
 * @param String name The name of the cookie.
 * @param String value The value of the cookie.
 * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
 * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
 *                             If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
 *                             If set to null or omitted, the cookie will be a session cookie and will not be retained
 *                             when the the browser exits.
 * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
 * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
 * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
 *                        require a secure protocol (like HTTPS).
 * @type undefined
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */

/**
 * Get the value of a cookie with the given name.
 *
 * @example $.cookie('the_cookie');
 * @desc Get the value of a cookie.
 *
 * @param String name The name of the cookie.
 * @return The value of the cookie.
 * @type String
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */
jQuery.cookie = function(name, value, options) {if (typeof value != 'undefined') {options = options || {};if (value === null) {value = '';options.expires = -1;}var expires = '';if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {var date;if (typeof options.expires == 'number') {date = new Date();date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));} else {date = options.expires;}expires = '; expires=' + date.toUTCString();}var path = options.path ? '; path=' + (options.path) : '';var domain = options.domain ? '; domain=' + (options.domain) : '';var secure = options.secure ? '; secure' : '';document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');} else {var cookieValue = null;if (document.cookie && document.cookie != '') {var cookies = document.cookie.split(';');for (var i = 0; i < cookies.length; i++) {var cookie = jQuery.trim(cookies[i]);if (cookie.substring(0, name.length + 1) == (name + '=')) {cookieValue = decodeURIComponent(cookie.substring(name.length + 1));break;}}}return cookieValue;}};



