document.domain = "instyle.com";//dev-wpcom.timeinc.net
var WRN = window.WRN || [];

IS.WRN = {
    documentinit : function($) {
        
        $('body').addClass('js');
        
        //searchbar listeners
        $('#findapostbydatelink, #findapostbydateicon').toggle(
            function() {
                $('#searchcalendar').slideDown();
                $('#searchkeyword').slideUp();
                $('#searchtag').slideUp();
                return false;
            },
            function() {
                $('#searchcalendar').slideUp();
                return false;
            }
        );
        $('#findapostbykeywordlink, #findapostbykeywordicon').toggle(
            function() {
                $('#searchkeyword').slideDown();
                $('#searchcalendar').slideUp();
                $('#searchtag').slideUp();
                return false;
            },
            function() {
                $('#searchkeyword').slideUp();
                return false;
            }
        );
        $('#findapostbytaglink, #findapostbytagicon').toggle(
            function() {
                $('#searchtag').slideDown();
                $('#searchcalendar').slideUp();
                $('#searchkeyword').slideUp();
                return false;
            },
            function() {
                $('#searchtag').slideUp();
                return false;
            }
        );
        
        //siteswelove default display
        $('#siteswelove li.linkcat:first').addClass('selected');
        var newheight = 0; //expands siteswelove container if necessary
        $('#siteswelove ul.xoxo').each(function() {
            if ($(this).height() > 70 && ($(this).height() - 70) > newheight) { newheight = $(this).height() - 70; }
        });
        if (newheight > 0){ $('#siteswelove').height($('#siteswelove').height() + newheight); }
        
        //siteswelove listeners
        $('#siteswelove li.linkcat h3.title').click(function() {
            $('#siteswelove li.linkcat').removeClass('selected');
            $(this).parent().addClass('selected');
            return false;
        });
	  
    } , 
	 // DDM New Gallery
	 photogallery : function($,postid,previous_post) { 
	
	   		$.getJSON('http://news.instyle.com/json-post-gallery-page/?postgallery=' + postid, function(data){
		    var gallery_title = data.title,
			gallery_id = data.id,
			gallery_slides = data.children,
			count = gallery_slides.length,
			url_string = window.location.search.substring(1),
			parameter = 'pageNum',
			split_url = url_string.split("&");
			for (i=0;i<split_url.length;i++) {
			    name_value_pair = split_url[i].split("=");
			
			    if (name_value_pair[0] == parameter) {
			    page_num = (name_value_pair[1] -1);
			    } else {
			    page_num = '0';
			    }		
			}
			// check for hash and adjust the page_num var
			var hash = (String(document.location).match('#') && String(document.location).split('#')[1] !== 'undefined') ? String(document.location).split('#')[1] : '';
            if ((hash != '') && (hash != 0) && (hash <= count)) {
                page_num = hash - 1;
			}
			// set up page
			$('div#slide h2.photoSubhead').html(''); //clear h2 slide title
			$('div#slide div.wrapper').html(''); // slide img and credit
			$('div#slide div.content').html(''); // slide caption
			$("div#slide <h2/>").attr("class","nolink").appendTo("div.content");
			$("div#slide <p/>").attr("class","photoSubhead").appendTo("div.content");
		
			var nextslide = Number(page_num) + 1,
			previousslide = Number(page_num) - 1,
			currentslide = nextslide;
			// if prev anchor missing add it
			if ($("div#slide div.prev a").length === 0) {
				$("<a/>").attr("href","javascript:void(0)").appendTo("div.prev");
			}
			
			// get and display the current slide
			$("<img/>").attr({src: gallery_slides[page_num].image, id: 'img' + page_num}).addClass("current-slide").appendTo("div#slide div.wrapper");
			// handle associated text & counter display
			$('div#slide .wp-credit-text').html(gallery_slides[page_num].credit); // slide credit
			$('div#slide h2.nolink').html(gallery_title); //gallery title
			$('div#slide p.photoSubhead').html(gallery_slides[page_num].title); //slide title
			$('div#slide div.pageof').html(currentslide + ' of ' + count); //counter
			$("div#slide div.prev a").attr('href', 'javascript:void(0)').addClass("jslink");
			$("div#slide div.next a").attr('href', 'javascript:void(0)').addClass('jslink');
			// check to see if this is the first slide if it is add & hide the prev if it is not load the prev slide 
			if (page_num != 0) {
				$("<img/>").attr({src: gallery_slides[previousslide].image, id: 'img' + previousslide}).addClass('jsimage').removeClass("current-slide").appendTo("div#slide div.wrapper");
			} else {
				$('div#slide div.prev a').hide();
			}
			// check to see that we are not at the last slide and load the next slide
			// if we are at the last slide switch out the button to next post
			if (page_num != (count - 1)) {
				$("<img/>").attr({src: gallery_slides[nextslide].image, id: 'img' + nextslide}).addClass("jsimage").appendTo("div#slide div.wrapper");
			}else {
				$("<a/>").attr("href","javascript:void(0)").appendTo("div.next");
				$('div#slide div.next').addClass("nextpost").css({ display: "block" });
				$('div#slide div.pageof').css({ width: "90px" });
			}
			IS.WRN.writeAds($,page_num);
			
			// onclick logic for next slide button
			$('div#slide div.next a').bind('click.killlink',function(event){
		    	event.preventDefault();
		    	page_num = Number(page_num) + 1;
				nextslide = Number(page_num) + 1;
				previousslide = Number(page_num) - 1;
				IS.Global.init();
				if (page_num == count && previous_post !== null) {
					window.location = 'http://news.instyle.com/?p=' + previous_post;
				}
				var loc = (String(document.location).match('#')) ? String(document.location).split('#')[0] : '';
                document.location = loc + '#' + nextslide;
				if ($("div#slide div.wrapper img#img" + page_num).length){ 
					$("div#slide div.wrapper img").removeClass("current-slide").addClass("jsimage");
					$("div#slide div.wrapper img#img" + page_num).addClass("current-slide").removeClass("jsimage");
				}else {
					$("div#slide div.wrapper img").removeClass("current-slide");
		    		$("<img/>").attr({src: gallery_slides[page_num].image, id: 'img' + page_num}).addClass("current-slide").removeClass("jsimage").appendTo("div#slide div.wrapper");	
				} 
				// pre-load next upcoming slide
				if (nextslide !== count && $("div#slide div.wrapper img#img" + (1+ page_num)).length == 0){
					$("<img/>").attr({src: gallery_slides[nextslide].image, id: 'img' + nextslide}).addClass("jsimage").appendTo("div#slide div.wrapper");
				}
				// handle associated text, counter and prev next button display
				$('div#slide div.wp-credit-text').html(gallery_slides[page_num].credit); // slide credit
				$('div#slide div.content p.photoSubhead').html(gallery_slides[page_num].title); //slide title
				$('div#slide .slidenav div.pageof').html((1 + page_num) + ' of ' + count); //counter
		    	if ((1 + page_num) === count) {
					if (("div.next a").length === 0){
					$("<a/>").attr("href","javascript:void(0)").appendTo("div.next");
					}
					$('div#slide div.next').addClass("nextpost").css({ display: "block" });
					$('div#slide div.pageof').css({ width: "90px" });
					$('div#slide div.prev a').css({ display: "block" });
		    	}  else {
					if (("div#slide div.prev a").length === 0){
					$("<a/>").attr("href","javascript:void(0)").appendTo("div.prev");
					}
					$('div#slide div.next a').css({ display: "block" });
					$('div#slide div.prev a').css({ display: "block" });
				}
				IS.WRN.writeAds($,page_num);
			});
			//onclick for previous button
			$('div#slide div.prev a').bind('click.killlink',function(event){
		    	event.preventDefault();
		    	page_num = Number(page_num) - 1;
				nextslide = Number(page_num) + 1;
				previousslide = Number(page_num) - 1;
				currentslide = nextslide;
				IS.Global.init();
				var loc = (String(document.location).match('#')) ? String(document.location).split('#')[0] : '';
                document.location = loc + '#' + nextslide;		
				if ($("div#slide div.wrapper img#img" + page_num).length){ 
					$("div#slide div.wrapper img").removeClass("current-slide").addClass("jsimage");
					$("div#slide div.wrapper img#img" + page_num).addClass("current-slide").removeClass("jsimage");
				} else {
					$("div#slide div.wrapper img").removeClass("current-slide");
					$("div#slide div.wrapper img").addClass("jsimage");
		    		$("<img/>").attr({src: gallery_slides[page_num].image, id: 'img' + page_num}).addClass("current-slide").appendTo("div#slide div.wrapper");
				}
					
				// // handle associated text, counter and prev next button display
				$('div#slide div.wp-credit-text').html(gallery_slides[page_num].credit); // slide credit
				$('div#slide div.content p.photoSubhead').html(gallery_slides[page_num].title); //slide title
				$('div#slide .slidenav div.pageof').html((1 + page_num) + ' of ' + count); //counter
				if (page_num === 0){
					if (("div#slide div.prev a").length === 0){
					$("<a/>").attr("href","javascript:void(0)").appendTo("div.prev");
					}
					$('div#slide div.prev a').hide();
					$('div#slide div.next a').css({ display: "block" });	
		    	} else {
					$('div#slide div.next a').css({ display: "block" });
						$('div#slide div.prev a').css({ display: "block" });	
				}
				if (currentslide !== count) {
						$('div#slide div.next').removeClass("nextpost");
						$('div#slide div.pageof').css({ width: "122px" });
				} else {
					$('div#slide div.nextpost').addClass("next");
					$('div#slide div.next').removeClass("nextpost");
					$('div#slide div.pageof').css({ width: "122px" });
					
				}
				IS.WRN.writeAds($,page_num);
			});
   		}); 
			
	},
    writeAds : function($,slidecount) {
		// delete the iframe elements, needed to address ad-ops rollover issue
			if ($('#iframe-headerad').length > 0 ) $('#iframe-headerad').remove();
			if ($('#col2 iframe').length > 0 ) $('#col2 iframe').remove();
			if ($('#iframe-footerad').length > 0 ) $('#iframe-footerad').remove();
			
			// find out if there is a query string, and if so, pass it into the iFrame
			var tempSearch = (location.search != '') ? location.search.replace("?", '') : '';
			var adopArr = tempSearch.split("&");	     
			if (adopArr) {
				var adop = '?';
				adopArrLen = adopArr.length - 1;
				var i;
				for (i = 0; i <= adopArrLen; i++) {
				var adop = adop + adopArr[i] + '&amp;';
				}		
			} else {
				var adop = '';		
			}
	
			// re-fire the top iFrame ad
			// DEV
			// var topAd = '<iframe height="90" frameborder="0" width="728" scrolling="no" allowtransparency="true" src="http://dev-wpcom.timeinc.net/whatsrightnow/wp-content/themes/whatsrightnow/whatsrigthnow728x90.html' + adop + '" id="iframe-headerad" style="width: 728px; height: 90px; "></iframe>';
			// PROD
			var topAd = '<iframe height="90" frameborder="0" width="728" scrolling="no" allowtransparency="true" src="http://www.instyle.com/instyle/static/h/iframe/ads/whatsrigthnow728x90.html' + adop + '" id="iframe-headerad" style="width: 728px; height: 90px; "></iframe>';
			$('#masthead div.headerad').append(topAd);
			
			// re-fire the side iFrame ad
			// DEV
			// var sideAd = '<iframe height="250" frameborder="0" width="336" scrolling="no" allowtransparency="true" src="http://dev-wpcom.timeinc.net/whatsrightnow/wp-content/themes/whatsrightnow/whatsrigthnow300x250.html' + adop + '" id="iframe-Right-Ad-#-1" style="width: 336px; height: 250px; "></iframe>';
			// PROD
			var sideAd = '<iframe height="250" frameborder="0" width="300" scrolling="no" allowtransparency="true" src="http://www.instyle.com/instyle/static/h/iframe/ads/whatsrigthnow300x250.html' + adop + '" id="iframe-Right-Ad-#-1" style="width: 300px; height: 250px; "></iframe>';
			$('div.ad div.content').append(sideAd);
        
        //rewrite omniture
        var strg = s_time.pageName.split('|gallery');
		var proxy = s_time.pageName;
		s_time.pageName = strg[0] + '|gallery|'  + (1 + Number(slidecount)) + strg[1];
		//alert('s_time.pageName:' + s_time.pageName);
        s_code = s_time.t();
        if(s_code)document.write(s_code);
		//rewrite comscore
		var u = location.href;     
		u = 'http://b.scorecardresearch.com/b?c1=2&c2=p-5dyPa639IrgIw&rn='+Math.random()+'&c7='+escape(u)+'&c3=&c4=&c5=&c6=&c10=&c15=&c16=&c8=&c9=&cv=1.7';
		$('body:first').append('<img src="'+u+'" width="1" height="1" border="0" />');
		s_time.pageName = proxy;
				
    }

}

jQuery(function($){
    IS.WRN.documentinit($);
});

function popEmailWin(url){var pageURL=(url)?url:document.URL;if(pageURL.substring(pageURL.length-1)=='#'){pageURL=pageURL.substring(0,pageURL.length-1);}if(pageURL.match('/quizzes/')){pageURL=transformURL(pageURL);}var pageTitle=self.document.title;if(pageTitle.indexOf('|')>0){pageTitle=pageTitle.substring(0,pageTitle.indexOf('|'));}var formURL='http://cgi.instyle.com/cgi-bin/mail/mailurl2friend.cgi?path=http://www.instyle.com/instyle/mail/templates/&url='+pageURL+'&group=instyle&title='+escape(pageTitle);showCenteredPopup('emailpop',formURL,'scrollbars=1',460,450);return false;}

if ((navigator.userAgent.match('iPhone')) || (navigator.userAgent.match('iPad')) || (navigator.userAgent.match('iPod')) || (location.search.indexOf('ipad=true') > -1)) {
	jQuery.getScript('http://img2-short.timeinc.net/instyle/static/j/ipad/hideflash.js');
}


