var $ = jQuery;
var share = {
    fb:function(url, title){
        if (url == undefined) {
            url = encodeURIComponent(window.location.toString());
        }
        if (title == undefined) {
            title = encodeURIComponent(document.title);
        }
        var sUrl = "http://www.facebook.com/sharer.php?u="+url+"&t="+title+"";
        ofb = window.open(sUrl,"fb", "width=600,height=300,menubar=no,location=no,resizable=yes,scrollbars=yes,status=yes");
        ofb.focus();
    },
    twitter:function(url, title){
        if (url == undefined) {
            url = encodeURIComponent(window.location.toString());
        }
        if (title == undefined) {
            title = encodeURIComponent(document.title);
        }
        var sUrl = "http://twitter.com/share?url="+url+"&text="+title+"";
        ofb = window.open(sUrl,"fb", "width=600,height=300,menubar=no,location=no,resizable=yes,scrollbars=yes,status=yes");
        ofb.focus();
    },
    stumble:function(url, title){
        if (url == undefined) {
            url = encodeURIComponent(window.location.toString());
        }
        if (title == undefined) {
            title = encodeURIComponent(document.title);
        }
        var sUrl = "http://www.stumbleupon.com/submit?url="+url+"&title="+title+"";
        ofb = window.open(sUrl,"fb", "width=600,height=300,menubar=no,location=no,resizable=yes,scrollbars=yes,status=yes");
        ofb.focus();
    },
    mixx:function(url, title){
        if (url == undefined) {
            url = encodeURIComponent(window.location.toString());
        }
        if (title == undefined) {
            title = encodeURIComponent(document.title);
        }
        var sUrl = "http://www.mixx.com/submit?page_url="+url+"&title="+title+"";
        ofb = window.open(sUrl,"fb", "width=600,height=300,menubar=no,location=no,resizable=yes,scrollbars=yes,status=yes");
        ofb.focus();
    },
    digg:function(url, title){
        if (url == undefined) {
            url = encodeURIComponent(window.location.toString());
        }
        if (title == undefined) {
            title = encodeURIComponent(document.title);
        }
        var sUrl = "http://digg.com/submit?url="+url+"&title="+title+"";
        ofb = window.open(sUrl,"fb", "width=600,height=300,menubar=no,location=no,resizable=yes,scrollbars=yes,status=yes");
        ofb.focus();
    },
    delicious:function(url, title){
        if (url == undefined) {
            url = encodeURIComponent(window.location.toString());
        }
        if (title == undefined) {
            title = encodeURIComponent(document.title);
        }
        var sUrl = "http://delicious.com/save?url="+url+"&title="+title+"";
        ofb = window.open(sUrl,"fb", "width=600,height=300,menubar=no,location=no,resizable=yes,scrollbars=yes,status=yes");
        ofb.focus();
    },
    reddit:function(url, title){
        if (url == undefined) {
            url = encodeURIComponent(window.location.toString());
        }
        if (title == undefined) {
            title = encodeURIComponent(document.title);
        }
        var sUrl = "http://reddit.com/submit?url="+url+"&title="+title+"";
        ofb = window.open(sUrl,"fb", "width=600,height=300,menubar=no,location=no,resizable=yes,scrollbars=yes,status=yes");
        ofb.focus();
    },
    myspace:function(url, title){
        if (url == undefined) {
            url = encodeURIComponent(window.location.toString());
        }
        if (title == undefined) {
            title = encodeURIComponent(document.title);
        }
        var sUrl = "http://www.myspace.com/Modules/PostTo/Pages/?u="+url+"&title="+title+"";
        ofb = window.open(sUrl,"fb", "width=600,height=300,menubar=no,location=no,resizable=yes,scrollbars=yes,status=yes");
        ofb.focus();
    },
    linkedin:function(url, title){
        if (url == undefined) {
            url = encodeURIComponent(window.location.toString());
        }
        if (title == undefined) {
            title = encodeURIComponent(document.title);
        }
        var sUrl = "http://www.linkedin.com/shareArticle?mini=true&url="+url+"&title="+title+"";
        ofb = window.open(sUrl,"fb", "width=600,height=300,menubar=no,location=no,resizable=yes,scrollbars=yes,status=yes");
        ofb.focus();
    }
};

var t2 = {
	compare:function(catid){
		$.get("/ajaxcompare.php?categoryid="+catid+"",function(data){ $('#mainctnt').html(data);});
	},
    getGallery:function(sType, iPage, sCategory, sShow, sView){
        if (sType == undefined) {
            sType = "photos";
        }
        if (iPage == undefined) {
            iPage = "2";
        }
        if (sCategory == undefined) {
            sCategory = "";
        }
        if (sShow == undefined) {
            sShow = "";
        }
        if (sView == undefined) {
            sView = "";
        }
        $("#gallerybox").load("ajaxgetgallery.php", {type:sType, page: iPage,categoryid:sCategory, show:sShow, view:sView}, function(){});        
    },
    print:function(id){
        var sUrl = "/story_print.php?id="+id+"";
        op = window.open(sUrl,"p", "top=10,screenY=20,left=100,screenX=200,outerWidth=670,width=570,innerWidth=470,menubar=no,location=no,resizable=yes,scrollbars=yes,status=yes");
        op.focus();
    },
    getCiFilter:function(divid, ciid){
        $('#'+divid).load('ajaxgetcifilter.php?cid='+ciid);
    },
    removecompare:function(id){
        var getCookies = $.cookie('cip');
        var cook = getCookies.split(',');
        var newArray = new Array();
        var i = 0;
        for (keys in cook) {
            if (parseInt(id)-1 != i) {
                newArray.push(cook[keys]);
            }
            i++;
        }
        $.cookie('cip',newArray.join(','));
        t2.compare(iCiCategoryid);
    },
    showHideReview:function(id){
        if (id=='rlt'){
            $('#rlt').addClass('active');
            $('#rtr').removeClass('active');
            $('#rrc').removeClass('active');

            $('#rltbox').removeClass('hidden');
            $('#rtrbox').addClass('hidden');
            $('#rrcbox').addClass('hidden');

        }
        if (id=='rtr'){
            $('#rtr').addClass('active');
            $('#rlt').removeClass('active');
            $('#rrc').removeClass('active');

            $('#rtrbox').removeClass('hidden');
            $('#rltbox').addClass('hidden');
            $('#rrcbox').addClass('hidden');
        }
        if (id=='rrc'){
            $('#rrc').addClass('active');
            $('#rtr').removeClass('active');
            $('#rlt').removeClass('active');

            $('#rrcbox').removeClass('hidden');
            $('#rltbox').addClass('hidden');
            $('#rtrbox').addClass('hidden');
        }
    }
};
var tech2Compare = t2;
/**
 * 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
 */

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) : '; 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;
    }
};

function writit(text,id){
for(i=1;i<=10;i++){
	tagid="rImage"+i;
	if(i % 2 == 1){
		document.getElementById(tagid).src="images/rstar1off.gif";
	}
	else{
		document.getElementById(tagid).src="images/rstar2off.gif";
	}
}
cnt=text*2;
for(i=1;i<=cnt;i++){
	tagid="rImage"+i;
	modi = i%2;
	if(modi == 0){
		document.getElementById(tagid).src="images/rstar2on.gif";
	}
	else{
		document.getElementById(tagid).src="images/rstar1on.gif";
	}
}
	disptext = text + " Star";
	if (document.getElementById)
	{
		x = document.getElementById(id);
		x.innerHTML = '';
		x.innerHTML = disptext;
	}
	else if (document.all)
	{
		x = document.all[id];
		x.innerHTML = disptext;
	}
	else if (document.layers)
	{
		x = document.layers[id];
		text2 = '<P CLASS="ntdBL">' + disptext + '</P>';
		x.document.open();
		x.document.write(text2);
		x.document.close();
	}
}

function clearrating(){
	for(i=1;i<=10;i++){
		tagid="rImage"+i;
		if(i % 2 == 1){
			document.getElementById(tagid).src="images/rstar1off.gif";
		}
		else{
			document.getElementById(tagid).src="images/rstar2off.gif";
		}
	}
	disptext = "";
	id="test"
	if (document.getElementById)
	{
		x = document.getElementById(id);
		x.innerHTML = '';
		x.innerHTML = disptext;
	}
	else if (document.all)
	{
		x = document.all[id];
		x.innerHTML = disptext;
	}
	else if (document.layers)
	{
		x = document.layers[id];
		text2 = '<P CLASS="ntdBL">' + disptext + '</P>';
		x.document.open();
		x.document.write(text2);
		x.document.close();
	}
}

function drawrating(x,y){
	prnt = "<ul>";
	for(i=1;i<=x;i++){
		prnt += "<li style=\"margin-left:5px;\"><img src=\"/images/video_star.jpg\" width=28 height=24 hspace=0 vspace=0 border=0></li>";
	}
	if(y > 0){
		prnt += "<li style=\"margin-left:5px;\"><img src=\"/images/video_halfstar.jpg\" width=28 height=24 hspace=0 vspace=0 border=0></li>";
	}
	else{
		y = 0;
	}
	prnt +=	"<li style=\"margin-left:25px;margin-top:5px;\" class=ntdBL>" + x + "." + y +" Stars</li>";
	document.getElementById("DynUsrRating").innerHTML = prnt;
	prnt += "</ul>";
}

function galleryRating(galId,val){
	if(galId != 0){
		jQuery.post(BASE_URL+'ajxupdategallery_rating.php', {'gid':galId,
												'rVal':val,
												'method':'ajax'},
		function(data){	jQuery('#galleryRateVal').val(val); writit(val,'test');	jQuery('#rateResultDiv').html("Thanks for rating.");});  
	}
}
function shareLink(id) {
   var state = document.getElementById(id).style.display;
    if (state == 'block') {
        document.getElementById(id).style.display = 'none';
		$('#shareImg').attr("src", 'images/share.gif');
    } else {
		$('#shareImg').attr("src", 'images/min_share.gif');
        document.getElementById(id).style.display = 'block';
    }
}

/**** To Validate Email Address*********/
function isValidEmail(str) {
	emailRe = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/
	if (!emailRe.test(str))	
		return false	
	else
		return true				
}

/**** varify and add newsletter email require in common header file *********/
function addnewsletteremail(){
	var mmnelval = 0;
	var dlnelval = 0;
	var wknelval = 0;
	var flag = 0;

	var mmnel = $('#mid_morning_newsletter').attr('checked');
	var dlnel = $('#daily_newsletter').attr('checked');
	var wknel = $('#weekly_newsletter').attr('checked');
	var mailID = $('#subscribe').val();

	
	if(mailID == 'Your Email Address' || mailID == ''){
		$('#invalid_error').html('Please Enter Your Email Address');
		$('#invalid_error').show();
		$('#subscribe').focus();
		flag=1;	
	}

	if(mmnel == false && dlnel == false && wknel == false){
		$('#invalid_error').html('Please Select Newsletter Type');
		$('#invalid_error').show();
		$('#mid_morning_newsletter').focus();
		flag=1;	
	}
	if(mmnel == true){
		mmnelval = 1;
	}

	if(dlnel == true){
		dlnelval = 1;
	}

	if(wknel == true){
		wknelval = 1;
	}

	if(mailID != '' && mailID != 'Your Email Address'){
		emailvalid=isValidEmail(mailID);	
		if(emailvalid!=true){
			$('#invalid_error').html('Please Enter Valid Email Address');
			$('#invalid_error').show();
			$('#subscribe').focus();			
			flag=1;		
		}
	}
	
	if(flag == 0){
		var datastrings = "emailID="+mailID+"&mmnewl="+mmnelval+"&dlnewl="+dlnelval+"&wknewl="+wknelval; //alert(datastrings);
		$.ajax({
               type: "POST",
               url: "/insertnewsmail.php",
               data: datastrings,
               success: function(data){ //alert(data);
               $('#invalid_error').show();
               $('#invalid_error').html(data);
               $('#subscribe').val('');
				   //alert(data);
				}
			});
		return true;
	}
}

