﻿/*
*   O2 More v5 (MVC)
*   Utility Scripts
*   AIS 2010
*   Author: M.Trunov
*/

/* DEBUG flag */
var debugOn = false;

/* Extend jQuery here */
jQuery.fn.extend({

	_trackGAurl: function (category) {

		var currentLocation = document.location + ""; //convert to string -MT
		var urlToTrack = (this.attr("href")) ? this.attr("href") : currentLocation;
		var linkText = $.trim($(this).text());

		_gaq.push(['_trackEvent', category, linkText, urlToTrack]);
		debug("GA call >> category: " + category + ", action: " + linkText + ", label: " + urlToTrack);
	},

	_trackGAlink: function (category) {

		var urlToTrack = (this.attr("href") && this.attr("href") != "#") ? this.attr("href") : "";

		var linkText = $.trim($(this).text());
		_gaq.push(['_trackEvent', category, linkText, urlToTrack]);
		debug("GA call >> category: " + category + ", action: " + linkText + ", label: " + urlToTrack);
	},

	_trackGAsocial: function (action, label) {

		var thisLabel = label ? label : false;
		if (!thisLabel && $(this).parents(".event-info-box").length > 0) {
			debug("hey");
			thisLabel = $.trim($(this).parents(".event-info-box").find("h3").text());
		} else if (!thisLabel && $(this).parents(".competition-summary").length > 0) {
			thisLabel = $.trim($(this).parent(".competition-summary").find("h2.competition-name").text());
		}

		_gaq.push(['_trackEvent', 'Social', action, thisLabel]);
		debug("GA call >> category: Social, action: " + action + ", label: " + thisLabel);
	}

});

/* Buttons lock function*/
/* Used on sign in ajax, register, forgot password, edit user details, edit interests, change password, alert me sections */
/* On ajax sign in called second time to unlock any button on the page, not required anyhere else */
function toggleBtnLock(state) {
	$(".button .primary input:visible").each(function () {
		if (state == "unlock") {
			debug("button unlock");
			$(this).removeAttr("disabled");
		} else {
			debug("button lock");
			$(this).attr("disabled", "disabled");
		}
	});

}

function toggleCheckBoxLock(state) {
    var chkBoxesList = $("#pref-slider-dropdown #sub-categories input:visible");
    var updateBox = $("#pref-slider-dropdown #prefUpdatePanel");
    if (state == "unlock") {
        debug("checkboxes unlock");
        chkBoxesList.each(function () {
            $(this).removeAttr("disabled");
            updateBox.css("display", "none");
        })
    } else {
        debug("checkboxes lock");
        chkBoxesList.each(function () {
            $(this).attr("disabled", "disabled");
            updateBox.css("display", "block");
        })
    }
}

//scroll to top function
function pageScrollTop() {
    debug("scrolling to page top");
    $('html,body').animate({ scrollTop: 0 }, 300);
};

// cookies handling -MT
// courtesy of quirksmode.org
function createCookie(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days * 60 * 60 * 1000)); //so it's hours now actually -MT
		var expires = "; expires=" + date.toGMTString();
	}
	else var expires = "";
	document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for (var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ') c = c.substring(1, c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name, "", -1);
}

// Console log for debugging -MT

function debug(logtext) {
	try {
		if (console && console.log && debugOn) {
			console.log(logtext);
		}
	}
	catch (err) {
		//alert(err);
	}
};

