$(function(){

    
    $(".adverts").wrapInner("<div class='advert_slider'></div>");
    $(".advert").show();
    
    if ($(".adverts").is(":visible")) {
        if (window.location=="/admin/cm/sites/222/") {
            $(".advert_slider").cycle({fx:"scrollVert",pause:1,random:0,timeout:10000000,easing:"easeInOutCubic",speed:2000000,prev:"#prev",next:"#next"});
        } else {
            $(".advert_slider").cycle({fx:"scrollVert",pause:1,random:0,timeout:8000,easing:"easeInOutCubic",speed:1500,prev:"#prev",next:"#next"});
        }   
    }  
    
   var currentURL = window.location; 
    /* Body Load */
    $(".MMDataCaptureForm_Table tr").each(function(){
        var label = $(".MMDataCaptureForm_LabelTD",this).text()+"<br/>";
        var input = $(".MMDataCaptureForm_Input",this);
        input.removeAttr("style").before(label).val("").wrap("<div class=\"mm_input\"></div>");
    });
    
    $(".content_left .advert:eq("+Math.floor(Math.random()*$(".content_left .advert").length)+")").show();
    $(".content_right .advert:eq("+Math.floor(Math.random()*$(".content_right .advert").length)+")").show();

    $(".advert a").attr("target","_blank");
        
    $("#home .articles .article:first").attr("style","padding-left: 0;"); 
    $("#home .articles .article:eq(2)").attr("style","padding-right: 0; border: none;"); 
    $(".members .team .member:last").attr("style","border: none; margin-bottom: 0; padding-bottom: 0px;");
  
    $(".cat").each(function(){
        var Category = $(this);
        if ($(this).text()=="Whats on") {$(this).html("<span>Whats on</span><img style='width: 85px; height: 23px;' src='http://www.seenmag.co.uk/res/builtin/whatson_button.gif'>").attr("style","display: inline;");}
        if ($(this).text()=="Food and drink") {$(this).html("<span>Food and drink</span><img style='width: 102px; height: 23px;' src='http://www.seenmag.co.uk/res/builtin/fooddrink_button.gif'>").attr("style","display: inline;");}
        if ($(this).text()=="Fashion") {$(this).html("<span>Fashion</span><img style='width: 70px; height: 23px;' src='http://www.seenmag.co.uk/res/builtin/fashion_button.gif'>").attr("style","display: inline;");}  
	    if ($(this).text()=="Lifestyle") {$(this).html("<span>Lifestyle</span><img style='width: 80px; height: 23px;' src='http://www.seenmag.co.uk/res/builtin/lifestyle_button.gif'>").attr("style","display: inline;");}  
	    if ($(this).text()=="Property") {$(this).html("<span>Property</span><img style='width: 80px; height: 23px;' src='http://www.seenmag.co.uk/res/builtin/property_button.gif'>").attr("style","display: inline;");} 
	    if ($(this).text()=="Celebrities") {$(this).html("<span>Celebrities</span><img style='width: 92px; height: 23px;' src='http://www.seenmag.co.uk/res/builtin/celebrities_button.gif'>").attr("style","display: inline;");}
	    if ($(this).text()=="Local issues") {$(this).html("<span>Local issues</span><img style='width: 101px; height: 23px;' src='http://www.seenmag.co.uk/res/builtin/local_issues_button.gif'>").attr("style","display: inline;");}
	    if ($(this).text()=="Reviews") {$(this).html("<span>Reviews</span><img style='width: 69px; height: 23px;' src='http://www.seenmag.co.uk/res/builtin/reviews_button.gif'>").attr("style","display: inline;");}    
        if ($(this).text()=="Feature") {$(this).html("<span>Feature</span><img style='width: 71px; height: 23px;' src='http://www.seenmag.co.uk/res/builtin/feature_button.gif'>").attr("style","display: inline;");}    
        if ($(this).text()=="Good grooming") {$(this).html("<span>Good Grooming</span><img style='width: 121px; height: 23px;' src='http://www.seenmag.co.uk/res/builtin/good_grooming_button.gif'>").attr("style","display: inline;");}
    });
    
    $(".issue .link a").attr("target","_blank");
    $(".search_result_item").fullLink();
 
    $(".mm_form p:first").addClass("instruction");
    $(".mm_form table tr").each(function(){
		$("td:first",this).addClass("label");
		$("td:last",this).addClass("input").children().addClass("textbox");
	});   
    $(".mm_form table .label").each(function(){
		var thisLabel = $(this);
		$("span",thisLabel).remove();
		var thisText = thisLabel.text();
		var newText = thisText.replace(/^\s+|\s+$/g, '');
		thisLabel.text(newText);
	}).wrapInner("<div></div>");
    $(".mm_form table input:last").addClass("SubmitButton").val("");
    $(".mm_form table textarea").parent().addClass("textareaTD");
    $(".mm_form table tr:last").addClass("SubmitButtonTR");
    $(".mm_form table td:last").addClass("SubmitButtonTD");
    
     $(".breadcrumb").each(function(){
        var charReplace = $(this).html();
        charReplace = charReplace.replace("&gt;","|");
        charReplace = charReplace.replace("&gt;","|");
        charReplace = charReplace.replace("&gt;","|");
        charReplace = charReplace.replace("&gt;","|");
        charReplace = charReplace.replace("&gt;","|");
        $(this).html(charReplace);
    });
        
    function populateDropdown(){
		var selectCategory = "<option value=\"null\">Select a category...</option>";
		$("#generic #article_filter_ddl").html("").append(selectCategory+"");
		Array.prototype.unique = function () {
			var r = new Array();
			o:for(var i = 0, n = this.length; i < n; i++) {
				for(var x = 0, y = r.length; x < y; x++) {
					if(r[x]==this[i]) {
						continue o;
					}
				}
				r[r.length] = this[i];
			}
			return r;
		}
		var categoryArray = new Array();
		for (y=0;y<$("#generic .article:visible").length;y++){
			var thisCategory = $("#generic .article:visible:eq("+y+")").children().children(".cat").text();
			var thisCategory = thisCategory.toLowerCase()
			var thisCategory = thisCategory.charAt(0).toUpperCase() + thisCategory.slice(1);
			categoryArray[y]="<option value=\""+thisCategory+"\">"+thisCategory+"</option>";
		}
		var unique = categoryArray.unique();
		var sort = unique.sort();
		var string = sort.toString();
		$("#generic #article_filter_ddl").append(string.replace(/,/g,"\n"));
	}
	populateDropdown();
	    
    $("#generic #article_filter_ddl").change(function(){
        var thisCategory = $("option:selected",this).val();
		if (thisCategory!="null") {
			$("#generic .article").hide();
			$("#generic .article .cat:contains('"+thisCategory+"')").parent().parent().fadeIn();
		} else {
			$("#generic .article").hide();
			$("#generic .article").fadeIn();
		}
    });
    
    var pagingItems = Math.ceil(eval($(".archive_issues .issue").length-3)/3);
    var countItems = 1;
    $(".paging").append("<div class='page'><strong>PAGE</strong></div>");
    while (countItems<=pagingItems+1) {
        $(".paging").append("<span id=\""+countItems+"\">"+countItems+"</span>");
        countItems++;
    }
    $(".paging span:first").addClass("active");
    $(".paging span").click(function(){
        $(".paging span").removeClass("active");
        $(this).addClass("active");
        var pageNumber=$(this).attr("id")-1;
        $(".archive_issues .holder .issue").animate({"top":"-"+eval(pageNumber*510)+"px"});

    }); 
    
    var pagingItems1 = Math.ceil(eval($(".articles_container .article, .holder .event").length-5)/5);
    var countItems = 1;
   
    while (countItems<=pagingItems1+1) {
        $(".paging").append("<span id=\""+countItems+"\">"+countItems+"</span>");
        countItems++;
    }
    $(".paging span:first").addClass("active");
    $(".paging span").click(function(){
        $(".paging span").removeClass("active");
        $(this).addClass("active");
        var pageNumber=$(this).attr("id")-1;
        $(".articles .articles_container .article").animate({"top":"-"+eval(pageNumber*820)+"px"});
        $(".whats_on .holder .event").animate({"top":"-"+eval(pageNumber*820)+"px"});
   }); 
   
   var pagingItems2 = Math.ceil(eval($(".gallery_listings_container .slider .small, .shop_listings_container .slider .small").length-3)/3);
    var countItems = 1;
   
    while (countItems<=pagingItems2+1) {
        $(".paging").append("<span id=\""+countItems+"\">"+countItems+"</span>");
        countItems++;
    }
    $(".paging span:first").addClass("active");
    $(".paging span").click(function(){
        $(".paging span").removeClass("active");
        $(this).addClass("active");
        var pageNumber=$(this).attr("id")-1;
        $(".gallery_listings_container .slider .small, .shop_listings_container .slider .small").animate({"left":"-"+eval(pageNumber*660)+"px"});
    });  
        
    var thisUrl = location.hostname;
    if (thisUrl=="www.seenmag.co.uk") {
        $(".videos .link a").each(function(){
            var url = $(this).attr("href");
            var urlLength = url.length;
            var charLength = eval(urlLength-11);
            var getCode = url.slice(charLength,urlLength);
            $(this).parent().html("<object width=\"640\" height=\"365\"><param name=\"movie\" value=\"http://www.youtube.com/v/"+getCode+"\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"http://www.youtube.com/v/"+getCode+"\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"640\" height=\"365\"></embed></object>");
        });
    }

    $(".archive_videos .link a").each(function(){
        var url = $(this).attr("href");
        var urlLength = url.length;
        var charLength = eval(urlLength-11);
        var getCode = url.slice(charLength,urlLength);
        $(this).parent().parent().children(":last").html("<span>"+getCode+"</span>");
    }).click(function(){
    return false;
    });

    $(".seen_tv .videos .link a").each(function(){
        var url = $(this).attr("href");
        var urlLength = url.length;
        var charLength = eval(urlLength-11);
        var getCode = url.slice(charLength,urlLength);
        $(this).parent().html("<object width=\"300\" height=\"182\"><param name=\"movie\" value=\"http://www.youtube.com/v/"+getCode+"\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"http://www.youtube.com/v/"+getCode+"\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"300\" height=\"182\"></embed></object>");
    });

    $(".archive_videos .archive .video").click(function(){
        var url = $(this,".url").text();
        var urlLength = url.length;
        var charLength = eval(urlLength-11);
        var getCode = url.slice(charLength,urlLength);
        $(".videos .link").hide().fadeIn().html("<object width=\"640\" height=\"365\"><param name=\"movie\" value=\"http://www.youtube.com/v/"+getCode+"\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"http://www.youtube.com/v/"+getCode+"\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"640\" height=\"365\"></embed></object>");
        $(".videos .video .title").text($(".title",this).text());
    });
    
    $(".with_event").live("click",function(){
		$.cookie("eventDate",$(this).attr("rel"),{path:"/"});
	});
    
    if ($.cookie("eventDate")) {
		$(".event").each(function(){
			var thisEvent = $(this);
			var clickedDay = $.cookie("eventDate").substr(0,2);
			var clickedMonth = $.cookie("eventDate").substr(3,2);
			var clickedYear = $.cookie("eventDate").substr(8,2);
			var clickedDate = clickedDay+"/"+clickedMonth+"/20"+clickedYear;
			var thisDate = $(".date",thisEvent).text();
			if (clickedDate==thisDate) { 
			    thisEvent.addClass("highlighted"); 
			    $(".event").hide();$(".highlighted").show(); 
			$(".paging span").hide();
			var pagingItems3 = Math.ceil(eval($(".highlighted").length-5)/5);
            var countItems = 1;
           
            while (countItems<=pagingItems3+1) {
                $(".paging").append("<span id=\""+countItems+"\">"+countItems+"</span>");
                countItems++;
            }
            $(".paging span:first").addClass("active");
            $(".paging span").click(function(){
                $(".paging span").removeClass("active");
                $(this).addClass("active");
                var pageNumber=$(this).attr("id")-1;
                $(".highlighted").animate({"top":"-"+eval(pageNumber*820)+"px"});
            });  
			}
        });
	}

    $(".date").each(function(){
        var origText=$(this).text();
        var splitText=origText.split("");
        $(this).html("<span class='month'>"+splitText[3]+ +splitText[4]+"</span>&nbsp;<span class='day'>"+splitText[0]+ +splitText[1]+"</span>");
    });
        
    $(".date .month").each(function(){
        var Month = $(this).text();
        if ($(this).text()=="01") {$(this).html("January");}
        if ($(this).text()=="02") {$(this).html("February");} 
        if ($(this).text()=="03") {$(this).html("March");} 
        if ($(this).text()=="04") {$(this).html("April");} 
        if ($(this).text()=="05") {$(this).html("May");} 
        if ($(this).text()=="06") {$(this).html("June");} 
        if ($(this).text()=="07") {$(this).html("July");}
        if ($(this).text()=="08") {$(this).html("August");} 
        if ($(this).text()=="09") {$(this).html("September");}
        if ($(this).text()=="10") {$(this).html("October");}
        if ($(this).text()=="11") {$(this).html("November");}
        if ($(this).text()=="12") {$(this).html("December");}
    }); 

    $(".archive_videos .video:odd").attr("style","margin-right: 0px;");
    $(".archive_videos .video .url").each(function(){
        var ArchiveVideoUrl = $(this).text();
    });
    
    $(".gallery_listings_container .small:first, .shop_listings_container .small:first").hide();
    $(".gallery_listings_container .small:nth-child(2), .shop_listings_container .small:nth-child(2)").attr("style","padding-left: 0; width: 190px;"); 
    //$(".gallery_listings_container .small:nth-child(2n+4), .shop_listings_container .small:nth-child:nth-child(2n+4)").attr("style","padding-right: 0; width: 190px; border: none;");
    //$(".gallery_listings_container .small:nth-child(3n+2), .shop_listings_container .small:nth-child:nth-child(3n+2)").attr("style","padding-left: 0; width: 190px;");  
    
       
    
    $(".gallery .photo:nth-child(3n+1)").attr("style","padding-left: 0;"); 
    //$(".gallery .photo:nth-child(2n)").attr("style","padding-left: 10px; background:red;"); 
    $(".gallery .photo:nth-child(3n)").attr("style","padding-right: 0px; border-right: 0px; background: none;"); 
  
    $(".gallery .photo").each(function(){
        var galleryImgUrl = $(".image img",this).attr("src"); 
        $(".image",this).wrapInner("<a href='"+galleryImgUrl+"'></a>");
    });    
  
    if ($(".gallery .photo").is(":visible")) {
        $(".gallery .photo .image a").lightBox(); 
        $(".twitter").hide(); 
    }
    
    $(".gallery object").attr("width","640").attr("height","480"); 
    $(".gallery embed").attr("width","640").attr("height","480");
    
    if ($(".whats_on").is(":visible")) {
        $("#calendar").show().wrapInner("<div class='calendar_container'></div>");
        $("#calendar").after("<div class='view_all_events'></div>");
        $(".view_all_events").click(function(){
            $(".event").show();
            $.cookie("eventDate",null,{path:'/'});
            $(".paging span").hide();
            var pagingItems1 = Math.ceil(eval($(".event").length-5)/5);
            var countItems = 1;
           
            while (countItems<=pagingItems1+1) {
                $(".paging").append("<span id=\""+countItems+"\">"+countItems+"</span>");
                countItems++;
            }
            $(".paging span:first").addClass("active");
            $(".paging span").click(function(){
                $(".paging span").removeClass("active");
                $(this).addClass("active");
                var pageNumber=$(this).attr("id")-1;
                $(".event").animate({"top":"-"+eval(pageNumber*820)+"px"});
           }); 
            
            
            
        }); 
    }   
    
    $(".calendar_container *").removeAttr("style").removeAttr("title");
	$(".calendar_container tr:nth-child(1) td:nth-child(1)").addClass("prev_month");
	$(".calendar_container tr:nth-child(1) td:nth-child(2)").addClass("month_title");
	$(".calendar_container tr:nth-child(1) td:nth-child(3)").addClass("next_month");
	$(".calendar_container tr:nth-child(2) td").addClass("calendar_day_title");
	$(".calendar_container tr:gt(2) td").addClass("calendar_day");
    $(".prev_month").removeAttr("bgcolor");
	$(".without_event").removeAttr("href");
    $(".calendar_day").removeClass("next_month").removeClass("prev_month").removeClass("month_title");
    $(".calendar_nextprev a").text("");
    
    $("#footer .links .right ul li a").click(function(){
        var categoryName=$(this).attr("title");
        $.cookie("category",categoryName,{path:'/'});
    }); 

    if ($.cookie("category")) {
        var categoryName=$.cookie("category");
        $(".articles_container .article").hide();
        $(".articles_container .article .right .cat span:contains('"+categoryName+"')").parent().parent().parent().fadeIn();
		$("#article_filter_ddl option").each(function(){ if ($(this).val()==categoryName) { $(this).attr("selected","selected"); } });
		$.cookie("category",null,{path:'/'});
    }
    
    
            
});

