/**
 * 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') { // name and value given, set cookie
        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(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        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 { // only name given, get cookie
        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]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
/*
 * jQuery Timer Plugin
 * http://www.evanbot.com/article/jquery-timer-plugin/23
 *
 * @version      1.0
 * @copyright    2009 Evan Byrne (http://www.evanbot.com)
 */ 

jQuery.timer = function(time,func,callback){
	var a = {timer:setTimeout(func,time),callback:null}
	if(typeof(callback) == 'function'){a.callback = callback;}
	return a;
};

jQuery.clearTimer = function(a){
	clearTimeout(a.timer);
	if(typeof(a.callback) == 'function'){a.callback();};
	return this;
};

// This will hold our timer
var myTimer = {};

$(document).ready(function() {
	function clear(el) {
		$(el).attr('value', ($(el).attr('value') == $(el).attr('title')) ? '' : $(el).attr('value'));
	}
	function set(el) {
		$(el).attr('value', ($(el).attr('value').length < 1) ? $(el).attr('title') : ($(el).attr('value')));
	}

	// override alert
	function alert(msg1,msg2) {
		$('#alert').jqmShow().find('div.jqmAlertContent').html(msg2);
		$('.jqmAlertTitle h1').html(msg1);
	}

	// override confirm
	// NOTE; A callback must be passed. It is executed on "cotinue". 
	//  This differs from the standard confirm() function, which returns
	//   only true or false!
	// If the callback is a string, it will be considered a "URL", and
	//  followed.
	// If the callback is a function, it will be executed.
	function confirm(msg,callback) {
		$('#confirm').jqmShow().find('p.jqmConfirmMsg').html(msg).end().find(':submit:visible').click(function(){
			if(this.value == 'yes') {
				//(typeof callback == 'string') ? window.location.href = callback : callback();
				$('#confirm').jqmHide();
			}
		});
	}

	$('#alert').jqm({overlay: 0, modal: true, trigger: false});
	$('a.alert').click(function() { 
		alert('You Have triggered an alert!'); 
		return false;
	});

	$('#confirm').jqm({overlay: 88, modal: true, trigger: false});
	$('a.confirm').click(function() { 
		confirm('About to visit: '+this.href+' !',this.href); 
		return false;
	});
	// submit + advertise
	$('#form-name').click(function() {				clear('#form-name');				});
	$('#form-name').focus(function() {				clear('#form-name');				});
	$('#form-name').blur(function() {				set('#form-name');					});
	$('#form-email').click(function() {				clear('#form-email');				});
	$('#form-email').focus(function() {				clear('#form-email');				});
	$('#form-email').blur(function() {				set('#form-email');					});
	$('#form-description').click(function() {		clear('#form-description');			});
	$('#form-description').focus(function() {		clear('#form-description');			});
	$('#form-description').blur(function() {		set('#form-description');			});
	$('#form-company_name').click(function() {		clear('#form-company_name');		});
	$('#form-company_name').focus(function() {		clear('#form-company_name');		});
	$('#form-company_name').blur(function() {		set('#form-company_name');			});

	// advertise
	$('#form-firstname').click(function() {			clear('#form-firstname');			});
	$('#form-firstname').focus(function() {			clear('#form-firstname');			});
	$('#form-firstname').blur(function() {			set('#form-firstname');				});
	$('#form-surname').click(function() {			clear('#form-surname');				});
	$('#form-surname').focus(function() {			clear('#form-surname');				});
	$('#form-surname').blur(function() {			set('#form-surname');				});
	$('#form-telephone_number').click(function() {	clear('#form-telephone_number');	});
	$('#form-telephone_number').focus(function() {	clear('#form-telephone_number');	});
	$('#form-telephone_number').blur(function() {	set('#form-telephone_number');		});
	$('#form-website').click(function() {			clear('#form-website');				});
	$('#form-website').focus(function() {			clear('#form-website');				});
	$('#form-website').blur(function() {			set('#form-website');				});

	// submit + advertise
	$('#form-username').click(function() {			clear('#form-username');			});
	$('#form-username').focus(function() {			clear('#form-username');			});
	$('#form-username').blur(function() {			set('#form-username');				});
	$('#form-password').click(function() {			clear('#form-password');			});
	$('#form-password').focus(function() {			clear('#form-password');			});
	$('#form-password').blur(function() {			set('#form-password');				});

	// tell a friend
	$('#form-sender').click(function() {			clear('#form-sender');				});
	$('#form-sender').focus(function() {			clear('#form-sender');				});
	$('#form-sender').blur(function() {				set('#form-sender');				});

	// feedback
	$('#form-feedback').click(function() {			clear('#form-feedback');			});
	$('#form-feedback').focus(function() {			clear('#form-feedback');			});
	$('#form-feedback').blur(function() {			set('#form-feedback');				});

	$('#form-subscribe').submit(function() {
		return process_submit();
	});
	$('#form-subscribe-submit').click(function() {
		$('#form-subscribe').submit();
		return false;
	});

	$('#form-feedbackform').submit(function() {
		return process_feedback();
	});
	$('#form-feedback-submit').click(function() {
		$('#form-feedbackform').submit();
		return false;
	});
	$('#form-advertise').submit(function() {
		return process_advertise();
	});
	$('#form-advertise-submit').click(function() {
		$('#form-advertise').submit();
		return false;
	});
	$('#form-rss').submit(function() {
		return process_rss();
	});
	$('#form-rss-submit').click(function() {
		$('#form-rss').submit();
		return false;
	});
	$('#form-login').submit(function() {
		return process_login();
	});
	$('#form-login-submit').click(function() {
		$('#form-login').submit();
		return false;
	});
	$('#form-tell-a-friend').submit(function() {
		return process_tell_a_friend();
	});
	$('#form-tell-a-friend-submit').click(function() {
		$('#form-tell-a-friend').submit();
		return false;
	});
	function process_submit () {
		var name = ($('#form-name').attr('value') == $('#form-name').attr('title')) ? '' : $('#form-name').attr('value');
		var email = ($('#form-email').attr('value') == $('#form-email').attr('title')) ? '' : $('#form-email').attr('value');
		var desc = ($('#form-description').attr('value') == $('#form-description').attr('title')) ? '' : $('#form-description').attr('value');
		var own = ($('#form-owner').is(':checked')) ? 1 : 0
		var comp =($('#form-company_name').attr('value') == $('#form-company_name').attr('title')) ? '' : $('#form-company_name').attr('value');

		if(name.length > 0 && email.length > 0 && desc.length > 0) {
			if(own == 1 && comp == '') {
				alert('Please fill in the company name if you are the owner, agent or re-seller');
				return false;
			} else {
				return true;
			}
		} else {
			var msg1 = 'Ooops!';
			var msg2 = 'Please tell me your name, email &amp; describe the product, cheers!'
			alert(msg1,msg2);
			return false;
		}
	}
	function process_feedback () {
		var name = ($('#form-name').attr('value') == $('#form-name').attr('title')) ? '' : $('#form-name').attr('value');
		var email = ($('#form-email').attr('value') == $('#form-email').attr('title')) ? '' : $('#form-email').attr('value');
		var feedback = ($('#form-feedback').attr('value') == $('#form-feedback').attr('title')) ? '' : $('#form-feedback').attr('value');

		if(name.length > 0 && email.length > 0 && feedback.length > 0) {
			return true;
		} else {
			var msg1 = 'Ooops!';
			var msg2 = 'Please tell me your name, email &amp; your feedback, cheers!'
			alert(msg1,msg2);
			return false;
		}
	}

	function process_advertise () {
		var firstname = ($('#form-firstname').attr('value') == $('#form-firstname').attr('title')) ? '' : $('#form-firstname').attr('value');
		var surname = ($('#form-surname').attr('value') == $('#form-surname').attr('title')) ? '' : $('#form-surname').attr('value');
		var telephone = ($('#form-telephone_number').attr('value') == $('#form-telephone_number').attr('title')) ? '' : $('#form-telephone_number').attr('value');
		var email = ($('#form-email').attr('value') == $('#form-email').attr('title')) ? '' : $('#form-email').attr('value');
		var company_name =($('#form-company_name').attr('value') == $('#form-company_name').attr('title')) ? '' : $('#form-company_name').attr('value');
		var website =($('#form-website').attr('value') == $('#form-website').attr('title')) ? '' : $('#form-website').attr('value');

		if(firstname.length > 0 && surname.length > 0 && telephone.length > 0 && email.length > 0) {
			return true;
		} else {
			var msg1 = 'Nearly Done!';
			var msg2 = 'Please let me know your name, surname, email and telephone number';
			alert(msg1,msg2);
			return false;
		}
	}

	function process_rss () {
		var name = ($('#form-name').attr('value') == $('#form-name').attr('title')) ? '' : $('#form-name').attr('value');
		var email = ($('#form-email').attr('value') == $('#form-email').attr('title')) ? '' : $('#form-email').attr('value');

		if(name.length > 0 && email.length > 0) {
			return true;
		} else {
			var msg1 = 'Ooops!';
			var msg2 = 'Please provide your name and email';
			alert(msg1,msg2);
			return false;
		}
	}

	function process_login() {
		var username = ($('#form-username').attr('value') == $('#form-username').attr('title')) ? '' : $('#form-username').attr('value');
		var password = ($('#form-password').attr('value') == $('#form-password').attr('title')) ? '' : $('#form-password').attr('value');

		if(username.length > 0 && password.length > 0) {
			return true;
		} else {
			var msg1 = 'Ooops!';
			var msg2 = 'Please provide your username and password';
			alert(msg1,msg2);
			return false;
		}
	}

	function process_tell_a_friend() {
		var sender = ($('#form-sender').attr('value') == $('#form-sender').attr('title')) ? '' : $('#form-sender').attr('value');
		var name = ($('#form-name').attr('value') == $('#form-name').attr('title')) ? '' : $('#form-name').attr('value');
		var email = ($('#form-email').attr('value') == $('#form-email').attr('title')) ? '' : $('#form-email').attr('value');
		var website =($('#form-website').attr('value').length < 1) ? '' : $('#form-website').attr('value');
		var desc = ($('#form-description').attr('value') == $('#form-description').attr('title')) ? '' : $('#form-description').attr('value');
		
		if(sender.length > 0 && name.length > 0 && email.length > 0 && website.length > 0 && desc.length > 0) {
			return true;
		} else {
			var msg1 = 'Easy Soldier!';
			var msg2 = 'Please tell me your name, your friends name &amp; email and write a little message.';
			alert(msg1,msg2);
			return false;
		}
	}
	$('.votehyperlink').click(function(){
		$.get($(this).attr('href')+'&ajax=1', function(data){
			var parts = data.split('|');
			var votedata = parts[2].split(';');
			var votetica = votedata[0].split(':');
			var votetoca = votedata[1].split(':');
			if(votetica[1] != 'null') {
				$('#tica-score .score-number a').html(votetica[1]);
			}
			if(votetoca[1] != 'null') {
				$('#toca-score .score-number a').html(votetoca[1]);
			}
			if($.cookie && $.cookie('votepopup') && $.cookie('votepopup')=='yes') {
				return false;
			} else {
				// Set the timer for 2 seconds
				myTimer = $.timer(1000,function(){
					$.clearTimer(myTimer);
					alert('',parts[0]+parts[1]);
					$('#vote-close').click(function(){
						if($('#donotshow').attr('checked')) {
							$.cookie('votepopup', 'yes');
						}
						$('#alert').jqmHide();
						return false;
					});
				});
			}
			return false;
		});
		return false;
	});
	$('.votehyperlink').click(function(){
		$.get($(this).attr('href')+'&ajax=1', function(data){
			var parts = data.split('|');
			var votedata = parts[2].split(';');
			var votetica = votedata[0].split(':');
			var votetoca = votedata[1].split(':');
			if(votetica[1] != 'null') {
				$('#tica-score .score-number a').html(votetica[1]);
			}
			if(votetoca[1] != 'null') {
				$('#toca-score .score-number a').html(votetoca[1]);
			}
			if($.cookie && $.cookie('votepopup') && $.cookie('votepopup')=='yes') {
				return false;
			} else {
				// Set the timer for 2 seconds
				myTimer = $.timer(1000,function(){
					$.clearTimer(myTimer);
					alert('',parts[0]+parts[1]);
					$('#vote-close').click(function(){
						if($('#donotshow').attr('checked')) {
							$.cookie('votepopup', 'yes');
						}
						$('#alert').jqmHide();
						return false;
					});
				});
				/*
				alert('',parts[0]+parts[1]);
				$('#vote-close').click(function(){
					if($('#donotshow').attr('checked')) {
						$.cookie('votepopup', 'yes');
					}
					$('#alert').jqmHide();
					return false;
				});
				*/
			}
			return false;
		});
		return false;
	});
//	$(document).pngFix( );
	$('body').supersleight({shim: '/img/shim.gif'});
	$('#close-old-browser').click(function(){
		$('#old-browser').fadeOut('slow');
	});
	var ie6=$.browser.msie&&($.browser.version == "6.0")&&!window.XMLHttpRequest;
	if(ie6 && !$.cookie('shown')) {
		$('#old-browser').fadeIn('slow');
		$.cookie('shown','true');
	}
});