/////////////////////////////////////////////////////////////////////////////////////////
////////// First things first ///////////////////////////////////////////////////////////


// Site Object
function Site() {
	for (i=0; i<arguments.length; i++) {
        var argArray = arguments[i].split('=');
        this[argArray[0]] = argArray[1];
    }
	if (typeof Site._init == 'undefined') {
		Site.prototype.initialize = function() {
			if (marieClaire.articleTypePrefix) {
				$('.viral_email').removeAttr('href').removeAttr('target').click( function() {
					marieClaire.emailFormWindow = window.open('/email-this' + marieClaire.sectionPath + marieClaire.urlName, 'emailForm', 'width=430,height=310,resizable=no,toolbar=no,location=no,directories=no,status=no,menubar=no,copyhistory=no');
					marieClaire.emailFormWindow.focus();
				});
			}
			switch (marieClaire.templateFileName.valueOf()) {
				case 'home':
					flash('promoplayer');
					break;
				case 'article' || 'user_contribution' || 'article_wide':
					checkForLogin();
					//if (marieClaire.articleTypePrefix.valueOf() == 'flipbook') flash('flipbook');					
					break;
				case 'horoscopes_article':
					if (marieClaire.sectionPrefix.valueOf() == 'coworker') getSecondSign(marieClaire.urlName);
					break;
				case 'registration':
					if (subjectUser.login == 0) {
							$('#subform').hide();
							$('#offer_term_checkbox').click( function() {
								subscribe();
							});
					}
					$('input:password').attr('value','');
					$('input:checkbox').attr('checked','');
					break;
				case 'community':					
					if(marieClaire.sectionPrefix) {
						switch (marieClaire.sectionPrefix.valueOf()) {
							case "profile":
								$('#submit_button').click(function() {communitySearch();});
								$('#friends_all, #friend_requests_all').hide();
								if (window.location.search.substring(1) == 'requests') $('#default_profile, #friend_requests_all').toggle();
//								if (subjectUser.login == 1) getIgnoreList(subjectUser.username);
								if ($('#profile .toggle_friend_requests')) {
									$('#profile .toggle_friend_requests').click(function() {
										$('#default_profile').fadeOut();
										$('#friend_requests_all').fadeIn();
									});
									$('#profile .toggle_friend_requests').removeAttr('href');
								};
								if ($('#friends .toggle_friends')) {
									$('#friends .toggle_friends').click(function() {
										$('#default_profile').fadeOut('fast');
										$('#friends_all').fadeIn('slow');
									});
									$('#friends .toggle_friends').removeAttr('href');
								};
								if ($('#friends_all .button')) {
									$('#friends_all .button').append('<a id="back_to_profile_friends" onclick="$(\'#friends_all\').fadeOut(\'fast\');$(\'#default_profile\').fadeIn(\'slow\');"><img src="/cm/marieclaire/images/v01/btn_cp_backtoprofile.gif" alt="back to profile" /></a>');
								}
								if ($('#back_to_profile_requests')) {
									$('#back_to_profile_requests').click(function() {
										$('#friend_requests_all').fadeOut();
										$('#default_profile').fadeIn();
									});
								};
								if ($('#friendship')) $('#friendship').click(function() {friend_request(objectUser,'add',this);});
								if ($('#request_block_' + objectUser)) $('#request_block_' + objectUser).click(function() {userBlockToggle(objectUser);});
								break;
							case "menu-iframe":
								if (parent.adjustIFrameSize) parent.adjustIFrameSize(window);
								break;
							case "profile_search1":
								var searchTerm = getQueryVariable('search_term');
								if (typeof searchTerm !== 'undefined') {
									memberFastSearch('/search/fast_profile_search?search_term=' + searchTerm);
								} else {
									memberFastSearch('/search/fast_profile_search?search_term=');
								}
								break;
							case 'forums':
								forumsRedirect();
								break;
							case 'pm':
								pmRedirect();
								break;
							case 'guestbook':
								guestbookRedirect();
								break;
						}
					}
				case 'flash_preview':
					try {
						if (marieClaire.urlName.valueOf() == 'pp') {
							marieClaire.promoPlayerURL = '/' + getQueryVariable('preview');
							$('body').append('<div id="promo_player"></div>');
							flash('promoplayer');
						}
					}
					finally { break; }
				case 'email_form':
					$('#lightbox').prepend('<a id="close_button"">CLOSE | X</a>');
					$('#close_button').click(function() {
						window.close();
					});
					break;
			}
			if ( marieClaire.flash != undefined && marieClaire.flash != 'false') {
				flash('magazinecover');
				if (typeof videoURL != 'undefined' && window.addEventListener) flash('videotout');
			}
			Site._init = true;
		}
	}
}

// Member/Visitor Object
function MemberProfile() {
    for (i=0; i<arguments.length; i++) {
        var argArray = arguments[i].split('=');
        this[argArray[0]] = argArray[1];
    }    
}


// Flash Embed Conditions
function flash(swfID) {
	var domID, swfSrc, swfWidth, swfHeight, swfVersion, swfBgcolor;
	var swfVersion = '9.0.45.0';
	var swfQuality = 'high';
	switch (swfID) {
		case 'magazinecover':
			switch (marieClaire.templateFileName.valueOf()) {
				case 'home':
					swfSrc = '/cm/marieclaire/flash/cover/cover_home.swf';
					swfWidth = '200';
					swfHeight = '113';
					domID = 'cover';
					break;
				default:
					swfSrc = '/cm/marieclaire/flash/cover/cover_chan.swf';
					swfWidth = '161';
					swfHeight = '187';
					domID = 'cover_image';
			}
			var soMagazineCover = new SWFObject(swfSrc, swfID, swfWidth, swfHeight, swfVersion, swfBgcolor);
			soMagazineCover.addParam('base', '/cm/marieclaire/flash/cover/');
			soMagazineCover.addParam('wmode','transparent');
			soMagazineCover.useExpressInstall('/cm/shared/ria/swfObject1.5/expressinstall.swf');
			soMagazineCover.write(domID);
			break;
		case 'promoplayer':
			var soPromoPlayer = new SWFObject('/cm/marieclaire/flash/promoplayer/v04/master_template.swf', swfID, '495', '309', swfVersion, '#ffffff');
			soPromoPlayer.addParam('scale', 'noscale');
			soPromoPlayer.addParam('base', '/cm/marieclaire/flash/promoplayer/v04/');
			soPromoPlayer.addParam("wmode","opaque");
			soPromoPlayer.addVariable("xmlfile", marieClaire.promoPlayerURL);
			soPromoPlayer.useExpressInstall('/cm/shared/ria/swfObject1.5/expressinstall.swf');
			soPromoPlayer.write('promo_player');
			break;
		case 'flipbook':
			var soFlipBook = new SWFObject('/cm/marieclaire/flash/flipbook/flipbook.swf', swfID, '400', '575', swfVersion, swfBgcolor);
			soFlipBook.addParam('base','/cm/marieclaire/flash/flipbook/');
			soFlipBook.addParam('wmode','transparent');
			soFlipBook.addVariable('xmlfile','/cm/marieclaire/flash' + marieClaire.sectionPath + 'flipbook/flipBookData-' + marieClaire.urlName + '.xml');				
			soFlipBook.useExpressInstall('/cm/shared/ria/swfObject1.5/expressinstall.swf');
			soFlipBook.write('flipbook_container');
			break;
		case 'videotout':
			switch (marieClaire.templateFileName.valueOf()) {				
				case 'home':
					swfSrc = '/cm/marieclaire/flash/tout_video/tout_video_home.swf';
					swfWidth = '215';
					swfHeight = '165';
					break;
				default:					
					swfSrc = '/cm/marieclaire/flash/tout_video/tout_video_chan.swf';
					swfWidth = '130';
					swfHeight = '100';
			}
			var soVideoTout = new SWFObject(swfSrc, swfID, swfWidth, swfHeight, swfVersion, swfBgcolor);
			soVideoTout.addParam('base', '/cm/marieclaire/flash/tout_video/');
			soVideoTout.addParam('wmode','transparent');
			soVideoTout.addVariable('videoURL', videoURL);
			soVideoTout.addVariable('clipImage1', clipImage1);
			soVideoTout.addVariable('clipImage2', clipImage2);
			soVideoTout.useExpressInstall('/cm/shared/ria/swfObject1.5/expressinstall.swf');
			soVideoTout.write('promo_image');
			break;		
	}
}

// Get query strings from url
function getQueryVariable(variable) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			return pair[1];
		}
	}
}

// Viral Toolbar - Kaboodle Script
function addToKaboodle() {
	var dkbdl=document;
	var skbdl=dkbdl.createElement('script');
	skbdl.type='text/javascript';
	skbdl.src='http://www.kaboodle.com/zg/addbutton.js';
	dkbdl.getElementsByTagName('head')[0].appendChild(skbdl);
}

/* Registration */
function getXML(urlString, callback) {
	$.ajax({
		url: urlString,
		dataType: "text",
		success: callback(data)
	});
}
/* /Registration */


//Check to see if user has just logged in
function checkForLogin() {
	if (getQueryVariable('commentLogin') == 1) {
		$('#post_comment').toggle();
	}
}

function closeCheck(){
	setTimeout("Reload();",00);	
	setTimeout("checkForComments();",400);
}


//Reloads Comments List <iframe>
function Reload(){
	if ($('#comment_list_iframe')) $('#comment_list_iframe').attr('src', $('#comment_list_iframe').attr('src'));
	if ($('#comment_count_iframe')) $('#comment_count_iframe').attr('src', $('#comment_count_iframe').attr('src'));
}

// Comment Checker
function checkForComments() {	
	var commentHeader = new String();
	if (marieClaire.articleTypePrefix.valueOf() == 'blog' || marieClaire.sectionPrefix.valueOf() == 'blog')  {
		commentHeader = '<div style="font-weight:normal; font-size:14px; text-align:center;">Be the first to post a comment!</div>';
	}
	if (document.getElementById('comment_list_iframe') && document.getElementById('comment_list_header')) {
		if (document.getElementById('comment_list_iframe').offsetHeight <= 35) {
			document.getElementById('comment_list_header').innerHTML = commentHeader;
			document.getElementById('comment_list_iframe').style.display = 'none';
			document.getElementById('comment_list_header').style.display = 'none';
		} else {
			document.getElementById('comment_list_header').style.display = 'block';
			document.getElementById('comment_list_iframe').style.display = 'auto';
			document.getElementById('comment_list_header').innerHTML = 'Comments';
			document.getElementById('article_comment').style.paddingBottom = '16px';
		}
	}
}

//Adjusts <iframe> Size According to Content
function adjustIFrameSize(iframeWindow) {
	var iframeElement = $('#' + iframeWindow.name);
	if (iframeWindow.document.height) {
		iframeElement.css('height', iframeWindow.document.height + 'px');
	}
	else if (document.all) {
		var iframeElement = document.all[iframeWindow.name];
		if (iframeWindow.document.compatMode && iframeWindow.document.compatMode != 'BackCompat') {
			iframeElement.style.height = iframeWindow.document.documentElement.scrollHeight + 'px';
		} else {
			iframeElement.style.height = iframeWindow.document.body.scrollHeight + 'px';
		}
	}
}
/* /Comments Display */

/* jQuery Extensions */

// Fade Toggler
jQuery.fn.extend({
	fadeToggle: function(speed, callback) {
          	if (this.css('display') != 'none') {
			this.fadeOut(speed, callback);
		} else {
			this.fadeIn(speed, callback);
		}		
	}
});

/* /jQuery Extensions */