// JavaScript Document

function fixContentHeights()
{
    if( $('.MainContent').length > 0 )
    {
        if($('.MainContent').outerHeight() <  $('#sidebar').outerHeight()) {
            // add to height
            padding = $('.MainContent').outerHeight() - $('.MainContent').height();
            newHeight = ( $('#sidebar').outerHeight() - padding) + 16
            $('.MainContent').css('height', newHeight+'px');
        }
        
    }

}

var gotErrors = 0;
$(document).ready(function() {

   fixContentHeights();

    // set lists to no border
    $('.left_stuff').each(function(index) {
        $('li',this).last().css('border-bottom', 'none');
    });
    $('.small_left_stuff').each(function(index) {
        $('li',this).last().css('border-bottom', 'none');
    });
    $('.feature').each(function(index) {
        $('li',this).last().css('border-bottom', 'none');
    });
    //$('.mainmenu .sublinks .left_stuff ul li:last').css('border', 'none');
     // do height of main content
    //console.log('maincontent ' + $('.MainContent').height());
    //console.log('sidebar ' + $('#sidebar').height());
    //
    // set border to none for last child of quick list
    $('.quick').each(function(index) {
        $('li',this).last().css('border-bottom', 'none');
    });
    $('.latestNews ul li:last').css('border', 'none');
    $('.rollover').each(function() {
     var imgFile = $(this).attr('src');
     var preloadImage = new Image;
     var imgExt = /(\.\w{3,4}$)/;
     preloadImage.src = imgFile.replace(imgExt, '_over$1');
     $(this).hover(
         function() {
            $(this).attr('src', preloadImage.src);
         },
         function() {
            $(this).attr('src', imgFile);
      });
     });
    $('.bigLink').each(function(index) {
        var lis = new Array()
        var i = 0;
            $('li',this).each(function(index) {
                var liStr = ''
                // get link
                var alink = $(this).find('a').attr('href');
                var heading = $(this).find('h3').text();
                var w = $(this).find('img').attr('width')
                var h = $(this).find('img').attr('height')
                var imSrc = $(this).find('img').attr('src')
                var imalt = $(this).find('img').attr('alt')
                var adate = $(this).find('.date').text()
                var p = $(this).find('p').not('.date').text()

                liStr = '<li><a href="'+alink+'">'
                liStr += '<h3>'+heading+'</h3> <span class="date">'+adate+'</span>'
                liStr += '<p>'+p+'<span class="readMore">Read more</span></p>'
                liStr += '<img src="'+imSrc+'" width="'+w+'" height="'+h+'" alt="'+imalt+'" class="l" /></a></li>'

                lis.push(liStr)
                // remove link
                $(this).remove()
              });
          // add new lis to ul
          for(i = 0; i < lis.length; i++)
          {
              $(this).append(lis[i])
          }
       });
    $('.bigLinkTwoCol').each(function(index) {
        var lis = new Array()
        var i = 0;
            $('li',this).each(function(index) {
                var liStr = ''
                // get link
                var alink = $(this).find('a').attr('href');
                var heading = $(this).find('h3').text();
                var w = $(this).find('img').attr('width')
                var h = $(this).find('img').attr('height')
                var imSrc = $(this).find('img').attr('src')
                var imalt = $(this).find('img').attr('alt')
                var adate = $(this).find('.date').text()
                var p = $(this).find('p').not('.date').text()

                liStr = '<li><a href="'+alink+'"><img src="'+imSrc+'" width="'+w+'" height="'+h+'" alt="'+imalt+'" />'
                liStr += '<h3>'+heading+'</h3> <span class="date">'+adate+'</span>'
                liStr += '<p>'+p+'<span class="readMore">Read more</span></p>'
                liStr += '</a></li>'
                lis.push(liStr)
                // remove link
                $(this).remove()
              });
          // add new lis to ul
          for(i = 0; i < lis.length; i++)
          {
              $(this).append(lis[i])
          }
       });

       $('.bigLinkItem').each(function(index) {
            var lis = new Array()
            var i = 0;
            // sub divs
            var adiv = $(this).find('div')
            $(adiv).each(function(index) {
                var str = ''
                var alink = $(this).find('a:first').attr('href');
                var aclass = $(this).attr('class');
                var heading = $(this).find('h2').text();
                var w = $(this).find('img').attr('width')
                var h = $(this).find('img').attr('height')
                var imSrc = $(this).find('img').attr('src')
                var imalt = $(this).find('img').attr('alt')
                var adate = $(this).find('.date').text()
                var p = $(this).find('p').not('.date').text()
                str = '<div class="'+aclass+'"><a href="'+alink+'">'
                str += '<h2>'+heading+'</h2>'
                str += '<p class="date">'+adate+'</p>'
                str += '<p>'+p+'<span class="readMore">Read more</span></p>'
                str += '<img width="'+w+'" height="'+h+'" alt="'+imalt+'" src="'+imSrc+'" class="l"></a></div>'
                lis.push(str)
                $(this).remove()
            });
           // add features
           for(i = 0; i < lis.length; i++)
           {
              $(this).append(lis[i])
           }
       });
       $('.main').each(function(index) {
            var lis = new Array()
            var i = 0;
            // sub divs
            var adiv = $(this).find('div')
            $(adiv).each(function(index) {
                var str = ''
                var alink = $(this).find('a:first').attr('href');
                var aclass = $(this).attr('class');
                var heading = $(this).find('h2').text();
                var w = $(this).find('img').attr('width')
                var h = $(this).find('img').attr('height')
                var imSrc = $(this).find('img').attr('src')
                var imalt = $(this).find('img').attr('alt')
                var adate = $(this).find('.date').text()
                var p = $(this).find('p').not('.date').text()
                str = '<div class="'+aclass+'"><a href="'+alink+'">'
                str += '<h2>'+heading+'</h2>'
                str += '<p class="date">'+adate+'</p>'
                str += '<p>'+p+'<span class="readMore">Read more</span></p>'
                str += '<img width="'+w+'" height="'+h+'" alt="'+imalt+'" src="'+imSrc+'" class="l"></a></div>'
                lis.push(str)
                $(this).remove()
            });
           // add features
           for(i = 0; i < lis.length; i++)
           {
              $(this).prepend(lis[i])
           }
       });
        $('.celebGrid').each(function(index) {
        var lis = new Array()
        var i = 0;
                $('li',this).each(function(index) {
                    var liStr = ''
                    // get link
                    var alink = $(this).find('a').attr('href');
                    var heading = $(this).find('h3').text();
                    var w = $(this).find('img').attr('width')
                    var h = $(this).find('img').attr('height')
                    var imSrc = $(this).find('img').attr('src')
                    var imalt = $(this).find('img').attr('alt')
                    var adate = $(this).find('.date').text()
                    var p = $(this).find('p').not('.date').text()

                    liStr = '<li><a href="'+alink+'"><img src="'+imSrc+'" width="'+w+'" height="'+h+'" alt="'+imalt+'" />'
                    liStr += '<p>'+p+'</p>'
                    liStr += '</a></li>'
                    lis.push(liStr)
                    // remove link
                    $(this).remove()
                });
                // add new lis to ul
                  for(i = 0; i < lis.length; i++)
                  {
                      $(this).append(lis[i])
                  }
       });
        $('.musicChart').each(function(index) {
        var lis = new Array()
        var i = 0;
                $('li',this).each(function(index) {
                    var liStr = ''
                    // get link
                    var alink = $(this).find('a').attr('href');
                    var heading = $(this).find('h3').text();
                    var imSrc = $(this).find('img:first').attr('src')
                    //var imalt = $(this).find('img:first').attr('alt')
                    var w = $(this).find('img:first').attr('width')
                    var h = $(this).find('img:first').attr('height')
                    var class1 = $(this).find('img:first').attr('class')
                    var imSrc2 = $(this).find('img:nth-child(2)').attr('src')
                    var imSrcCP = $(this).find('.chartPos').attr('src')
                    //var imalt2 = $(this).find('img:nth-child(2)').attr('alt')
                    var w2 = $(this).find('img:nth-child(2)').attr('width')
                    var h2 = $(this).find('img:nth-child(2)').attr('height')
                    var class2 = $(this).find('img:nth-child(2)').attr('class')
                    liStr = '<li><a href="'+alink+'"><h3>'+heading+'</h3>'
                    liStr += '<img src="'+imSrc+'" width="'+w+'" height="'+h+'" alt="movie" class="'+class1+'" />'
                    liStr += '<img width="29" height="38" class="chartPos" alt="#1" src="'+ imSrcCP+'">'
                    liStr += '</a></li>'
                    lis.push(liStr)
                    // remove link
                    $(this).remove()
                });
                // add new lis to ul
                  for(i = 0; i < lis.length; i++)
                  {
                      $(this).append(lis[i])
                  }
       });
	// hide labels
	$('#homepage #loveAndSex .yyForm label').each(function(index) {
	   $(this).hide();
	});
        // show labels
	$('.showLabel').each(function(index) {
	   $(this).show();
	});
        // show labels for yin yang ask
	$('.askForm label').each(function(index) {
	   $(this).show();
	});
	// test for placeholder
	jQuery.support.placeholder = false;
	test = document.createElement('input');
	if('placeholder' in test) jQuery.support.placeholder = true;

	if(!$.support.placeholder) {
		// set up textarea
		$('textarea').each(function(index) {
                    if($(this).val().length == 0) {
			do_placeHolder($(this));
                    }
 	    });
            // set up inputs
	    $('input[type="text"]').each(function(index) {
            if($(this).attr('id') != 'q') {
                     if($(this).val().length == 0) {
                        do_placeHolder($(this));
                     }
                }
 	    });
		// for input
		$('input').focus(function() {
                    if($(this).attr('id') != 'q') {
			if ($(this).attr('placeholder') != '' && $(this).val() == $(this).attr('placeholder')) {
				$(this).val('').removeClass('hasPlaceholder');
			}
                    }
		});
		$('input').blur(function() {
                    if($(this).attr('id') != 'q') {
			if ($(this).attr('placeholder') != '' && ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))) {
				$(this).val($(this).attr('placeholder')).addClass('hasPlaceholder');
			}
                    }
		});
		// for textarea
		$('textarea').focus(function() {
			if ($(this).attr('placeholder') != '' && $(this).val() == $(this).attr('placeholder')) {
				$(this).val('').removeClass('hasPlaceholder');
			}
		});
		$('textarea').blur(function() {
			if ($(this).attr('placeholder') != '' && ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))) {
				$(this).val($(this).attr('placeholder')).addClass('hasPlaceholder');
			}
		});
	}// end placeholder stuff 
        $('.askForm').submit(function () {
            var gotErrors = 0;
                if(!$.support.placeholder) {
                        $('.askForm input[type="text"]').each(function(index) {
                            //alert($(this).attr('name') + $(this).val());
                                if ($(this).val() == $(this).attr('placeholder')) {
                                        $(this).val('');
                                         do_error($(this),1);
                                        gotErrors++;
                                }
                        });
                        $('.askForm textarea').each(function(index) {
                             //alert($(this).attr('name') + $(this).val());
                                if ($(this).val() == $(this).attr('placeholder')) {
                                        $(this).val('');
                                         do_error($(this),1);
                                         gotErrors++;
                                }
                        });
                }// end placeholder stuff
                if(gotErrors == 0) {
                        return true;
                } else {
                        return false;
                }
        }); // end submit form

$('input[type="text"]').blur(function (event) {
    do_error($(this),0);
});
$('textarea').blur(function (event) {
    do_error($(this),0);
});
$('#SubscribeDaily').change(function(event) {
   if((checkCheck($('#SubscribeDaily'))== false) && (checkCheck($('#SubscribeMonthly')) == false)) {
        displayError(1, $('#check_Error'));
    } else {
        displayError(0, $('#check_Error'));
    }
});
$('#SubscribeMonthly').change(function(event) {
   if((checkCheck($('#SubscribeDaily'))== false) && (checkCheck($('#SubscribeMonthly')) == false)) {
        displayError(1, $('check_Error'));
    } else {
        displayError(0, $('check_Error'));
    }
});
 
});
// functions
function do_error(obj,num)
{
	var $input_type = $(obj).attr('title');
	$input_type = $input_type.split('_');
	if($input_type[1] == 'req') {
		if(whichOne($(obj),$input_type[0],num) == true) {
			var $error_div = '#' + $(obj).attr('name') + '_Error';
			displayError(1, $error_div);
			if(num == 1) {
                            do_placeHolder($(obj));
			}
			gotErrors++;
		} else {
			var $error_div = '#' + $(obj).attr('name') + '_Error';
			displayError(0, $error_div);
		}
	}
}
function do_placeHolder(obj)
{
	if ($(obj).attr('placeholder') != '') {
		$(obj).val($(obj).attr('placeholder'));
		$(obj).addClass('hasPlaceholder')
	}
}
function whichOne(obj,input_type,num) {
	var error = true;
	switch (input_type.toLowerCase()) {
		case 'name':
			(checkInput(obj,num) == true) ? error = true : error = false;
			return error;
			break;
                case 'comment':
			(checkComment(obj,num) == true) ? error = true : error = false;
			return error;
			break;
		case 'email':
			(checkEmail(obj) == true) ? error = true : error = false;
			return error;
			break;
		case 'phone':
			(checkPhone(obj) == true) ? error = true : error = false;
			return error;
		case 'mobile':
			(checkMobile(obj) == true) ? error = true : error = false;
			return error;
		case 'phone_mobile':
			(checkPhoneMobile(obj) == true) ? error = true : error = false;
			return error;
			break;
		case 'number':
			(checkNumber(obj) == true) ? error = true : error = false;
			return error;
			break;
	}
}

/**** validation functions **************/

	function checkInput(obj,num) {
	// num == 1 means that form has been sent
		if($.support.placeholder || num == 1) {
			if($(obj).val().length <= 3) {
				return true;
			} else {
				return false;
			}
		} else {
			if (($(obj).val() == $(obj).attr('placeholder')) || ($(obj).val() <= 3)) {
				return true;
			} else {
				return false;
			}
		}
	}
        function checkComment(obj,num) {
	// num == 1 means that form has been sent
		if($.support.placeholder || num == 1) {
			if($(obj).val().length <= 20) {
				return true;
			} else {
				return false;
			}
		} else {
			if (($(obj).val() == $(obj).attr('placeholder')) || ($(obj).val() <= 20)) {
				return true;
			} else {
				return false;
			}
		}
	}
	function checkSelect(obj) {
		if($(obj).attr('selectedIndex') == 0) {
			return true;
		} else {
			return false;
		}
	}
	function checkRadio(obj) {
		if(!$(obj).attr('checked')) {
			return false;
		} else {
			return true;
		}
	}
	function checkEmail(obj) {
		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (filter.test($(obj).val())) {
			return false;
		} else {
			return true;
		}
	}
	function checkNumber(obj) {
		var invalid = 0;
		//strip out spaces
		var stripped = $(obj).val().replace(/^\s+|\s+$/g, '');
		if (isNaN(parseInt(stripped))) {
			invalid = 1;
		}
		if (invalid == 1) {
			return false;
		} else {
			return true;
		}
	}
	function checkPhone(obj) {
		var invalid = 0;
		// UK phone number format
		var filter  =  /^(0|44)[1278]\d{9}$/;
		//strip out acceptable non-numeric characters
		var stripped = $(obj).val().replace(/[\(\)\.\-\ ]/g, '');
		if (isNaN(parseInt(stripped))) {
			invalid = 1;
		} else {
			// now check format
			if (filter.test(stripped)) {
				invalid = 0;
			} else {
				invalid = 1;
			}
		}
		if (invalid == 1) {
			return false;
		} else {
			return true;
		}
	}
	function checkMobile(obj) {
		var invalid = 0;
		// UK mobile phone number format
		var filter  =  /07\d{9}/;
		//strip out acceptable non-numeric characters
		var stripped = $(obj).val().replace(/[\(\)\.\-\ ]/g, '');
		if (isNaN(parseInt(stripped))) {
			invalid = 1;
		} else {
			// now check format
			if (filter.test(stripped)) {
				invalid = 0;
			} else {
				invalid = 1;
			}
		}
		if (invalid == 1) {
			return false;
		} else {
			return true;
		}
	}
	function checkPhoneMobile(obj) {
		var invalidPhone = 0;
		var invalidMobile = 0;
		// UK phone number format
		var filterPhone  =  /^(0|44)[1278]\d{9}$/;
		// UK mobile phone number format
		var filterMobile  =  /07\d{9}/;
		//strip out acceptable non-numeric characters
		var stripped = $(obj).val().replace(/[\(\)\.\-\ ]/g, '');
		if (isNaN(parseInt(stripped))) {
			invalidPhone = 1;
			invalidMobile = 1;
		}
		// now check format
		if (filterPhone.test(stripped)) {
			invalidPhone = 0;
		} else {
			invalidPhone = 1;
		}
		// now check format
		if (filterMobile.test(stripped)) {
			invalidMobile = 0;
		} else {
			invalidMobile = 1;
		}
		if (invalidPhone == 1 || invalidPhone == 1) {
			return false;
		} else {
			return true;
		}
	}
	function checkCheck(obj) {
            //alert($(obj).attr('checked'))
		if($(obj).attr('checked') == false) {
			return false;
		} else {
			return true;
		}
	}
	function displayError(num, errorDiv) {
		if(num == 1) {
			$(errorDiv).css('display','block');
		} else {
			$(errorDiv).css('display','none');
		}
	}
