﻿/* Global JS */
$(document).ready(function() {

    $('.rollover').bind("mouseover", function(e) {
        $(this).attr("src", $(this).attr("src").replace(/_off/, "_over"));
    });
    $('.rollover').bind("mouseout", function(e) {
        $(this).attr("src", $(this).attr("src").replace(/_over/, "_off"));
    });
    $('.rollover.active').each(function(e) {
        $(this).attr("src", $(this).attr("src").replace(/_off/, "_over"));
        $(this).unbind("mouseover mouseout");
    });


    $('#lookbook .thumbs div').each(function(e) {
        if ($(this).hasClass('current')) {
            $(this).children('.shade').css('display', 'block');
        }
        $(this).bind('click', function(e) {
            swap_pic($(this));
        });
    });

    function swap_pic(el) {
        var pic = el.children('img').attr('name');

        $('#lookbook .looks .look').each(function() {
            $(this).css('display', 'none');
        });
        $('#lookbook .looks .look').children('img.detail').each(function() {
            $(this).css('zIndex', '-1');
        });
        $('#lookbook_' + pic).css('display', 'block');

        $('#lookbook .thumbs div.current')
			.removeClass('current')
			.children('.shade').css('display', 'none');

        el
			.addClass('current')
			.children('.shade').css('display', 'block');
    }


    $('#lookbook img.big').hover(
		function() {
		    $(this).siblings('.detailset').css('display', 'block');

		    if ($(this).siblings('.detailset').children('.plusmarker').css('display') == 'none')  //need this to stop the blinky
		        $(this).siblings('.detailset').children('.plusmarker').fadeIn('slow');
		},
		function() {
		    //$(this).siblings('.detailset').css('display','none');
		}
	);


    $('#ctl00_containerWrapper_news_signup_form_button').click(function() {

        var postal = $('#news_signup_form .postal-1').val() + ' ' + $('#news_signup_form .postal-2').val();
        var email = $('#news_signup_form .email').val();

        //strip out any html tags
        email = email.replace(/<\/?[^>]+>/gi, '');
        postal = postal.replace(/<\/?[^>]+>/gi, '');

        var urlString = '?email=' + email + '&postalcode=' + postal;
        $('#news_signup_form .email').val('');
        $('#news_signup_form .postal-1').val('');
        $('#news_signup_form .postal-2').val('');

        popupNewsLetterSignUp('newsletter_signup/' + urlString);


    });

    $('a .fb').hover(
		function() {
		    $(this).parent().siblings('div.shareonfacebook').show();
		},
		function() {
		    $(this).parent().siblings('div.shareonfacebook').hide();
		}
	);

    $('img.plusmarker').hover(
		function() {
		    //if($(this).siblings('img.pricebubble').attr('rel') != 'finito'){
		    //$(this).siblings('img.pricebubble').show();
		    //DD_belatedPNG.fix('img.pricebubble');
		    //$(this).siblings('img.pricebubble').attr('rel','finito');
		    //}
		    $(this).siblings('img.pricebubble').show();
		},
		function() {
		    $(this).siblings('img.pricebubble').hide();
		}
	);

    $('#lookbook div.thumbs').hover(
		function() {
		    $('.plusmarker').fadeOut("slow");
		},
		function() {
		    //$(this).siblings('.detailset').css('display','none');
		}
	);


    $('.plusmarker').each(function(e) {
        $(this).bind('click', function() {
            $(this).siblings('.pricebubble').css('display', 'block');

        });
    });


    $('a.price-toggle').click(
		function() {
		    $(this).siblings('a').children('img.rollover').css('display', 'block');
		    $(this).siblings('a').children('img.price-is-on').css('display', 'block');
		}
	);



    $('a.zoom-toggle').click(
		function() {
		    $('.plusmarker').fadeOut("slow");
		    $(this).siblings('div.zoom').css('display', 'block');
		    $(this).siblings('img.big').css('display', 'none');
		    $(this).children('img.rollover').css('display', 'none');
		    $(this).siblings('a').children('img.price-is-on').css('display', 'none');
		    if ($('.zoom_active').length > 0) {
		        $('.mover').remove();
		    } else {
		        $('.zoom').addClass('zoom_active');
		        $(".zoom_active").anythingZoomer({
		            expansionSize: 0,
		            speedMultiplier: 2
		        });
		    }
		}
	);




    $('a.price-toggle').click(
		function() {
		    $('.mover').remove();
		    $(this).siblings('div.zoom').css('display', 'none');
		    $(this).siblings('img.big').css('display', 'block');

		}
	);


    $('.shot .expand-en, .shot a .expandsm-en').hover(
		function() {
		    $(this).attr('src', '/en/images/whatshot/see_style.gif');
		},
		function() {
		    $(this).attr('src', '/images/buttons/plus.gif');
		}
	);

    $('.shot .expand-fr, .shot a .expandsm-fr').hover(
		function() {
		    //$(this).attr('src', '/fr/images/whatshot/see_style.gif');
		    $(this).attr('src', '/fr/images/whatshot/see_style.gif');
		},
		function() {
		    $(this).attr('src', '/images/buttons/plus.gif');
		}
	);

    // Fashion Week Playlist
    $('div.fashion-week-playlist-item').hover(
		function() {
		    $(this).addClass('fashion-week-playlist-item-hover');
		}, function() {
		    $(this).removeClass('fashion-week-playlist-item-hover');
		}
	);
    $('div.fashion-week-playlist-item').click(
		function() {
		    var selected_episode = $(this).attr("id").replace(/playlist_/, "");
		    $('div.fashion-week-playlist-item').each(function(e) {
		        var thumbnail = $(this).children('div.thumbnail').children('img');
		        thumbnail.attr('src', thumbnail.attr('src').replace('_on', '_off'));
		    });
		    $('.fashion-week-video-title-image,.fashion-week-video-desc-image').each(function(e) {
		        $(this).css("display", "none");
		    });
		    var thumbnail = $(this).children('div.thumbnail').children('img');
		    thumbnail.attr('src', thumbnail.attr('src').replace('_off', '_on'));
		    $('#' + selected_episode + '_title').fadeIn('slow');
		    $('#' + selected_episode + '_desc').fadeIn('slow');

		    $('#video_player_flash')[0].playVideo($(this).attr('videoUrl'), { "facebook": encodeURIComponent("http://www.facebook.com/share.php?u=http://www.joe.ca/en/fresh_411/fashion_week/" + $(this).attr('video')), "twitter": encodeURIComponent("http://twitter.com/home?status=http://www.joe.ca/en/fresh_411/fashion_week/" + $(this).attr('video')) });
		    /*$('#video_player_flash')[0].playVideo($(this).attr('videoUrl'),"");
		    $('#video_player_flash')[0].setTwitter("http://twitter.com/home?status="+encodeURIComponent("http://www.joe.ca/en/whats_hot/fashion_week/"+$(this).attr('video')));
		    $('#video_player_flash')[0].setFacebook("http://www.facebook.com/share.php?u="+encodeURIComponent("http://www.joe.ca/en/whats_hot/fashion_week/"+$(this).attr('video')));*/
		}
	);

});

function showTermsOfService() {
    tb_show("", "#TB_inline?width=800&inlineId=termsOfService", "");
}

function showWallpapers() {
    tb_show("", "#TB_inline?width=800&inlineId=downloadWallpaper", "");
}

function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        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);
}




function myPopup(myPage, width, height) {
    if (!width) width = 780;
    if (!height) height = 580;
    var wnd = window.open(myPage, 'JoeFreshStyle' + Math.round(10000 * Math.random()), 'width=' + width + ', height=' + height + ',location=no, menubar=no, status=no, toolbar=no, resizable=no, scrollbars=yes');
    wnd.focus();
}

function popupNewsLetterSignUp(url) {
    var wnd = window.open(url, 'JoeFreshStyleNewsLetter', 'width=550,height=520,location=no,menubar=no,status=no,toolbar=no,resizable=no,scrollbars=no');
    wnd.focus();
}

